-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode290.java
More file actions
33 lines (26 loc) · 813 Bytes
/
leetcode290.java
File metadata and controls
33 lines (26 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
class leetcode290 {
public boolean wordPattern(String pattern, String s) {
HashMap<String, Character> map =new HashMap<>();
HashMap<Character, String> map2=new HashMap<>();
String words[]=s.split(" ");
if (words.length != pattern.length()) return false;
for(int i=0;i<words.length;i++){
String str=words[i];
char ch=pattern.charAt(i);
if(map.containsKey(str)){
if(map.get(str)!=ch){
return false;
}
}
else{
if(map2.containsKey(ch)){
return false;
}
map.put(str,ch);
map2.put(ch,str);
}
}
return true;
}
}