-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordPattern.java
More file actions
21 lines (21 loc) · 968 Bytes
/
WordPattern.java
File metadata and controls
21 lines (21 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public boolean wordPattern(String pattern, String str) {
HashMap<Character,String> charMapping = new HashMap<Character, String>();
HashMap<String, Character> strMapping = new HashMap<String, Character>();
String[] words = str.split(" ");
if(words.length != pattern.length()) return false;
int n = words.length;
for(int i = 0; i < n; i++) {
char current = pattern.charAt(i);
String word = words[i].strip();
if(charMapping.containsKey(current) && strMapping.containsKey(word)) {
if(!charMapping.get(current).equals(word) || strMapping.get(word) != current)
return false;
} else if(!charMapping.containsKey(current) && !strMapping.containsKey(word)) {
charMapping.put(current, word);
strMapping.put(word, current);
} else return false;
}
return true;
}
}