-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
25 lines (21 loc) · 827 Bytes
/
Solution.java
File metadata and controls
25 lines (21 loc) · 827 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
class Solution {
public boolean wordPattern(String pattern, String str) {
Map<String, Character> wordsMap = new HashMap<String, Character>();
Map<Character, String> pMap = new HashMap<Character, String>();
String[] words = str.split(" ");
if (pattern.length() != words.length) {
return false;
}
for (int i = 0; i < words.length; i++) {
String word = words[i];
Character p = pattern.charAt(i);
if (wordsMap.get(word) == null && pMap.get(p) == null) {
wordsMap.put(word, p);
pMap.put(p, word);
} else if (!p.equals(wordsMap.get(word)) || !word.equals(pMap.get(p))) {
return false;
}
}
return true;
}
}