-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
29 lines (24 loc) · 852 Bytes
/
Solution.java
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
// https://leetcode.com/problems/word-pattern
class Solution {
public boolean wordPattern(String pattern, String str) {
HashMap<Character, String> map = new HashMap<>();
HashMap<String, Character> map2 = new HashMap<>();
String arr[] = str.split(" ");
if(arr.length != pattern.length()){
return false;
}
for(int i=0;i<pattern.length();i++){
char ch = pattern.charAt(i);
String s = arr[i];
if(map.containsKey(ch) || map2.containsKey(s)){
if(map.containsKey(ch) != map2.containsKey(s) || !map.get(ch).equals(s)){
return false;
}
}else{
map.put(ch, s);
map2.put(s, ch);
}
}
return true;
}
}