-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
42 lines (35 loc) · 1.21 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Solution {
public boolean wordPattern(String pattern, String s) {
Map<Character, String> map = new HashMap<>();
int n = pattern.length();
List<String> ar = Arrays.stream(s.split(" ")).collect(Collectors.toList());
if (n != ar.size())
return false;
boolean out = true;
for (int i = 0; i < n; i++) {
if (map.containsKey(pattern.charAt(i))) {
if (!map.get(pattern.charAt(i)).equals(ar.get(i)))
out = false;
} else {
if (map.containsValue(ar.get(i)) && getKeyByValue(map, ar.get(i)) != pattern.charAt(i)) {
return false;
} else {
map.put(pattern.charAt(i), ar.get(i));
}
}
}
return out;
}
private Character getKeyByValue(Map<Character, String> map, String value) {
for (Map.Entry<Character, String> e : map.entrySet()) {
if (e.getValue().equals(value))
return e.getKey();
}
return null;
}
}