-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet290.java
More file actions
31 lines (28 loc) · 893 Bytes
/
Copy pathleet290.java
File metadata and controls
31 lines (28 loc) · 893 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
class Solution {
public boolean wordPattern(String pattern, String s) {
HashMap <Character,String> h1 = new HashMap<>();
HashMap <String,Character> h2 = new HashMap<>();
String[] words = s.split(" ");
int n = pattern.length();
if(pattern.length() != words.length) return false;
for(int i = 0 ; i < n ; i++){
char c = pattern.charAt(i);
String str = words[i];
if(h1.containsKey(c)){
if(!Objects.equals(h1.get(c),str)){
return false;
}
}else{
h1.put(c,str);
}
if(h2.containsKey(str)){
if(!Objects.equals(h2.get(str),c)){
return false;
}
}else{
h2.put(str,c);
}
}
return true;
}
}