|
3 | 3 | // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Design #Trie |
4 | 4 | // #Level_2_Day_16_Design #Udemy_Trie_and_Heap |
5 | 5 | // #Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) |
6 | | -// #2024_11_15_Time_30_ms_(99.78%)_Space_55.1_MB_(72.51%) |
7 | | - |
8 | | -import java.util.Arrays; |
| 6 | +// #2024_11_15_Time_32_ms_(95.05%)_Space_54.9_MB_(91.16%) |
9 | 7 |
|
10 | 8 | @SuppressWarnings("java:S1104") |
11 | 9 | public class Trie { |
12 | | - private boolean ans = false; |
13 | | - private final TrieNode[] trees = new TrieNode[26]; |
| 10 | + private final TrieNode root; |
| 11 | + private boolean startWith; |
14 | 12 |
|
15 | | - TrieNode mapWordToTree(TrieNode t, String word, int i) { |
16 | | - char m = word.charAt(i); |
17 | | - boolean found = false; |
18 | | - TrieNode a = t.nexts[m - 'a']; |
19 | | - if (a != null) { |
20 | | - if (i != word.length() - 1) { |
21 | | - mapWordToTree(a, word, i + 1); |
22 | | - } else { |
23 | | - a.end = true; |
24 | | - } |
25 | | - found = true; |
26 | | - } |
27 | | - if (!found) { |
28 | | - TrieNode prev = t; |
29 | | - for (int j = i; j < word.length(); j++) { |
30 | | - TrieNode temp = new TrieNode(word.charAt(j)); |
31 | | - prev.nexts[word.charAt(j) - 'a'] = temp; |
32 | | - prev = temp; |
33 | | - } |
34 | | - prev.end = true; |
| 13 | + private static class TrieNode { |
| 14 | + // Initialize your data structure here. |
| 15 | + public TrieNode[] children; |
| 16 | + public boolean isWord; |
| 17 | + |
| 18 | + public TrieNode() { |
| 19 | + children = new TrieNode[26]; |
35 | 20 | } |
36 | | - return t; |
37 | 21 | } |
38 | 22 |
|
| 23 | + public Trie() { |
| 24 | + root = new TrieNode(); |
| 25 | + } |
| 26 | + |
| 27 | + // Inserts a word into the trie. |
39 | 28 | public void insert(String word) { |
40 | | - char a = word.charAt(0); |
41 | | - if (trees[a - 'a'] == null) { |
42 | | - TrieNode t = new TrieNode(a); |
43 | | - trees[a - 'a'] = t; |
44 | | - } |
45 | | - if (1 == word.length()) { |
46 | | - trees[a - 'a'].end = true; |
47 | | - return; |
48 | | - } |
49 | | - trees[a - 'a'] = mapWordToTree(trees[a - 'a'], word, 1); |
| 29 | + insert(word, root, 0); |
50 | 30 | } |
51 | 31 |
|
52 | | - public boolean searchWordInTree(TrieNode t, String word, int i) { |
53 | | - char a = word.charAt(i); |
54 | | - TrieNode m = t.nexts[a - 'a']; |
55 | | - if (m != null) { |
56 | | - if (i == word.length() - 1) { |
57 | | - ans = true; |
58 | | - return m.end; |
59 | | - } |
60 | | - return searchWordInTree(m, word, i + 1); |
| 32 | + private void insert(String word, TrieNode root, int idx) { |
| 33 | + if (idx == word.length()) { |
| 34 | + root.isWord = true; |
| 35 | + return; |
| 36 | + } |
| 37 | + int index = word.charAt(idx) - 'a'; |
| 38 | + if (root.children[index] == null) { |
| 39 | + root.children[index] = new TrieNode(); |
61 | 40 | } |
62 | | - return false; |
| 41 | + insert(word, root.children[index], idx + 1); |
63 | 42 | } |
64 | 43 |
|
| 44 | + // Returns if the word is in the trie. |
65 | 45 | public boolean search(String word) { |
66 | | - char a = word.charAt(0); |
67 | | - if (trees[a - 'a'] == null) { |
68 | | - return false; |
69 | | - } else { |
70 | | - if (1 == word.length()) { |
71 | | - return trees[a - 'a'].end; |
72 | | - } |
73 | | - return searchWordInTree(trees[a - 'a'], word, 1); |
74 | | - } |
| 46 | + return search(word, root, 0); |
75 | 47 | } |
76 | 48 |
|
77 | | - public boolean startsWith(String prefix) { |
78 | | - char a = prefix.charAt(0); |
79 | | - ans = false; |
80 | | - if (trees[a - 'a'] == null) { |
| 49 | + private boolean search(String word, TrieNode root, int idx) { |
| 50 | + if (idx == word.length()) { |
| 51 | + startWith = true; |
| 52 | + return root.isWord; |
| 53 | + } |
| 54 | + int index = word.charAt(idx) - 'a'; |
| 55 | + if (root.children[index] == null) { |
| 56 | + startWith = false; |
81 | 57 | return false; |
82 | | - } else { |
83 | | - if (1 == prefix.length()) { |
84 | | - return true; |
85 | | - } |
86 | | - searchWordInTree(trees[a - 'a'], prefix, 1); |
87 | 58 | } |
88 | | - return ans; |
| 59 | + return search(word, root.children[index], idx + 1); |
89 | 60 | } |
90 | 61 |
|
91 | | - static class TrieNode { |
92 | | - char val; |
93 | | - boolean end = false; |
94 | | - TrieNode[] nexts = new TrieNode[26]; |
95 | | - |
96 | | - TrieNode(char val) { |
97 | | - this.val = val; |
98 | | - } |
99 | | - |
100 | | - @Override |
101 | | - public String toString() { |
102 | | - return val + " " + Arrays.toString(nexts) + " " + end; |
103 | | - } |
| 62 | + // Returns if there is any word in the trie |
| 63 | + // that starts with the given prefix. |
| 64 | + public boolean startsWith(String prefix) { |
| 65 | + search(prefix); |
| 66 | + return startWith; |
104 | 67 | } |
105 | 68 | } |
106 | 69 |
|
|
0 commit comments