-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddAndSearchWordDataStructureDesign.java
88 lines (75 loc) · 1.8 KB
/
AddAndSearchWordDataStructureDesign.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package string;
import java.util.*;
/**
* @author Shogo Akiyama
* Solved on 10/03/2019
*
* 211. Add and Search Word - Data structure design
* https://leetcode.com/problems/add-and-search-word-data-structure-design/
* Difficulty: Medium
*
* Approach: Trie
* Runtime: 94 ms, faster than 26.33% of Java online submissions for Add and Search Word - Data structure design.
* Memory Usage: 59.1 MB, less than 60.61% of Java online submissions for Add and Search Word - Data structure design.
*
* @see StringTest#testAddAndSearchWordDataStructureDesign()
*/
public class AddAndSearchWordDataStructureDesign {
private Trie root = new Trie();
/** Adds a word into the data structure. */
public void addWord(String word) {
root.add(word, 0);
}
/**
* Returns if the word is in the data structure. A word could contain the dot
* character '.' to represent any one letter.
*/
public boolean search(String word) {
return root.search(word, 0);
}
class Trie {
boolean isWord;
Map<Character, Trie> nexts;
Trie() {
isWord = false;
nexts = new HashMap<Character, Trie>();
}
void add(String s, int index) {
if (s == null) {
return;
}
if (s.length() <= index) {
isWord = true;
return;
}
char c = s.charAt(index);
if (!nexts.containsKey(c)) {
nexts.put(c, new Trie());
}
nexts.get(c).add(s, ++index);
}
boolean search(String s, int index) {
if (s == null) {
return false;
}
if (s.length() <= index) {
return isWord;
}
char c = s.charAt(index);
if (c == '.') {
boolean flag = false;
for (Trie t : nexts.values()) {
if (flag) {
return true;
}
flag = t.search(s, index + 1);
}
return flag;
}
if (nexts.containsKey(c)) {
return nexts.get(c).search(s, ++index);
}
return false;
}
}
}