|
1 |
| -class TrieNode { |
2 |
| - private Map<Character, TrieNode> children; |
3 |
| - private boolean isWord; |
4 |
| - |
5 |
| - public TrieNode() { |
6 |
| - children = new HashMap<>(); |
7 |
| - isWord = false; |
8 |
| - } |
9 |
| - |
10 |
| - public boolean isWord() { |
11 |
| - return isWord; |
12 |
| - } |
13 |
| - |
14 |
| - public void isFullWord() { |
15 |
| - isWord = true; |
16 |
| - } |
17 |
| - |
18 |
| - public void putCharIfAbsent(char c) { |
19 |
| - children.putIfAbsent(c, new TrieNode()); |
20 |
| - } |
21 |
| - |
22 |
| - public TrieNode getChild(char c) { |
23 |
| - return children.get(c); |
24 |
| - } |
25 |
| -} |
26 |
| - |
27 | 1 | class Trie {
|
28 | 2 | private TrieNode root;
|
29 | 3 |
|
30 |
| - /** Initialize your data structure here. */ |
31 | 4 | public Trie() {
|
32 | 5 | root = new TrieNode();
|
33 | 6 | }
|
34 | 7 |
|
35 |
| - /** Inserts a word into the trie. */ |
36 | 8 | public void insert(String word) {
|
37 |
| - if (word == null) { |
38 |
| - return; |
39 |
| - } |
| 9 | + TrieNode runner = root; |
40 | 10 |
|
41 |
| - TrieNode curr = root; |
42 | 11 | for (char c : word.toCharArray()) {
|
43 |
| - curr.putCharIfAbsent(c); |
44 |
| - curr = curr.getChild(c); |
| 12 | + runner.children.putIfAbsent(c, new TrieNode()); |
| 13 | + runner = runner.children.get(c); |
45 | 14 | }
|
46 | 15 |
|
47 |
| - curr.isFullWord(); |
| 16 | + runner.isWord = true; |
48 | 17 | }
|
49 | 18 |
|
50 |
| - /** Returns if the word is in the trie. */ |
51 | 19 | public boolean search(String word) {
|
52 |
| - if (word == null) { |
53 |
| - return false; |
54 |
| - } |
55 |
| - |
56 |
| - TrieNode curr = root; |
| 20 | + TrieNode runner = root; |
57 | 21 |
|
58 | 22 | for (char c : word.toCharArray()) {
|
59 |
| - curr = curr.getChild(c); |
60 |
| - |
61 |
| - if (curr == null) { |
| 23 | + if (runner.children.get(c) == null) { |
62 | 24 | return false;
|
63 | 25 | }
|
| 26 | + |
| 27 | + runner = runner.children.get(c); |
64 | 28 | }
|
65 | 29 |
|
66 |
| - return curr.isWord(); |
| 30 | + return runner.isWord; |
67 | 31 | }
|
68 | 32 |
|
69 |
| - /** |
70 |
| - * Returns if there is any word in the trie that starts with the given prefix. |
71 |
| - */ |
72 | 33 | public boolean startsWith(String prefix) {
|
73 |
| - if (prefix == null) { |
74 |
| - return false; |
75 |
| - } |
76 |
| - |
77 |
| - TrieNode curr = root; |
| 34 | + TrieNode runner = root; |
78 | 35 |
|
79 | 36 | for (char c : prefix.toCharArray()) {
|
80 |
| - curr = curr.getChild(c); |
81 |
| - if (curr == null) { |
| 37 | + if (runner.children.get(c) == null) { |
82 | 38 | return false;
|
83 | 39 | }
|
| 40 | + |
| 41 | + runner = runner.children.get(c); |
84 | 42 | }
|
85 | 43 |
|
86 | 44 | return true;
|
87 | 45 | }
|
| 46 | + |
| 47 | + private class TrieNode { |
| 48 | + private Map<Character, TrieNode> children; |
| 49 | + private boolean isWord; |
| 50 | + |
| 51 | + public TrieNode() { |
| 52 | + children = new HashMap<>(); |
| 53 | + isWord = false; |
| 54 | + } |
| 55 | + } |
88 | 56 | }
|
0 commit comments