| 22 | } |
| 23 | |
| 24 | class Trie { |
| 25 | TrieNode root = new TrieNode(); |
| 26 | |
| 27 | void insert(String word) { |
| 28 | TrieNode current = root; |
| 29 | for (int i = 0; i < word.length(); i++) { |
| 30 | char c = word.charAt(i); |
| 31 | if (!current.hasChild(c)) { |
| 32 | current.children[c - 'a'] = new TrieNode(); |
| 33 | } |
| 34 | current = current.getChild(c); |
| 35 | } |
| 36 | current.isEnd = true; |
| 37 | } |
| 38 | |
| 39 | boolean checkWord(String word) { |
| 40 | TrieNode current = root; |
| 41 | for (int i = 0; i < word.length(); i++) { |
| 42 | char c = word.charAt(i); |
| 43 | if (current.hasChild(c)) { |
| 44 | current = current.getChild(c); |
| 45 | } else { |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | return current.isEnd; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | class TrieNode { |
| 54 | TrieNode[] children = new TrieNode[26]; |
nothing calls this directly
no outgoing calls
no test coverage detected