-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_290.java
31 lines (28 loc) · 1023 Bytes
/
_290.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
30
31
package com.fishercoder.solutions.firstthousand;
import java.util.HashMap;
import java.util.Map;
public class _290 {
public static class Solution1 {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
char[] patterns = pattern.toCharArray();
Map<Character, String> map = new HashMap();
if (patterns.length != words.length) {
return false;
}
for (int i = 0; i < patterns.length; i++) {
if (map.containsKey(patterns[i])) {
if (!map.get(patterns[i]).equals(words[i])) {
return false;
}
} else {
if (map.containsValue(words[i])) {
return false; // this is for this case: "abba", "dog dog dog dog"
}
map.put(patterns[i], words[i]);
}
}
return true;
}
}
}