forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
48 lines (43 loc) · 1.34 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
43
44
45
46
47
48
class MagicDictionary {
private Set<String> words;
private Map<String, Integer> counter;
/** Initialize your data structure here. */
public MagicDictionary() {
words = new HashSet<>();
counter = new HashMap<>();
}
public void buildDict(String[] dictionary) {
for (String word : dictionary) {
words.add(word);
for (String p : patterns(word)) {
counter.put(p, counter.getOrDefault(p, 0) + 1);
}
}
}
public boolean search(String searchWord) {
for (String p : patterns(searchWord)) {
int cnt = counter.getOrDefault(p, 0);
if (cnt > 1 || (cnt == 1 && !words.contains(searchWord))) {
return true;
}
}
return false;
}
private List<String> patterns(String word) {
List<String> res = new ArrayList<>();
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; ++i) {
char c = chars[i];
chars[i] = '*';
res.add(new String(chars));
chars[i] = c;
}
return res;
}
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dictionary);
* boolean param_2 = obj.search(searchWord);
*/