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
- // #2022_06_28_Time_34_ms_ (99.90%)_Space_51_MB_(94.92 %)
6
+ // #2024_11_15_Time_30_ms_ (99.78%)_Space_55.1_MB_(72.51 %)
7
7
8
8
@ SuppressWarnings ("java:S1104" )
9
9
public class Trie {
10
- private final TrieNode root ;
11
- private boolean startWith ;
12
-
13
- private static class TrieNode {
14
- // Initialize your data structure here.
15
- public TrieNode [] children ;
16
- public boolean isWord ;
10
+ boolean ans =false ;
11
+ TrieNode [] trees =new TrieNode [26 ];
12
+ public Trie () {
17
13
18
- public TrieNode () {
19
- children = new TrieNode [26 ];
20
- }
21
14
}
22
15
23
- public Trie () {
24
- root = new TrieNode ();
16
+ TrieNode mapWordToTree (TrieNode t , String word , int i ){
17
+ char m =word .charAt (i );
18
+ boolean found =false ;
19
+ TrieNode a =t .nexts [m -'a' ];
20
+ if (a !=null ){
21
+ if (i !=word .length ()-1 ){
22
+ mapWordToTree (a ,word ,i +1 );
23
+ found =true ;
24
+
25
+ }else {
26
+ a .end =true ;
27
+ found =true ;
28
+ }
29
+ }
30
+ if (!found ){
31
+ TrieNode prev =t ;
32
+ for (int j =i ;j <word .length ();j ++){
33
+ TrieNode temp =new TrieNode (word .charAt (j ));
34
+ prev .nexts [word .charAt (j )-'a' ]=temp ;
35
+ prev =temp ;
36
+ }
37
+ prev .end =true ;
38
+ }
39
+ return t ;
25
40
}
26
41
27
- // Inserts a word into the trie.
28
42
public void insert (String word ) {
29
- insert (word , root , 0 );
43
+ char a =word .charAt (0 );
44
+ if (trees [a -'a' ]==null ){
45
+ TrieNode t =new TrieNode (a );
46
+ trees [a -'a' ]=t ;
47
+ if (1 ==word .length ()){
48
+ trees [a -'a' ].end =true ;
49
+ return ;
50
+ }
51
+ trees [a -'a' ]=mapWordToTree (trees [a -'a' ],word ,1 );
52
+ }else {
53
+ if (1 ==word .length ()){
54
+ trees [a -'a' ].end =true ;
55
+ return ;
56
+ }
57
+ trees [a -'a' ]=mapWordToTree (trees [a -'a' ],word ,1 );
58
+ }
59
+ //System.out.println(trees[a-'a']);
30
60
}
61
+ public boolean searchWordInTree (TrieNode t , String word , int i ){
62
+ char a =word .charAt (i );
63
+ TrieNode m =t .nexts [a -'a' ];
64
+ if (m !=null ){
65
+
66
+ if (i ==word .length ()-1 ){
67
+ ans =true ;
68
+ if (m .end ){
69
+ return true ;
70
+ }else {
71
+ return false ;
72
+ }
73
+ }
74
+ return searchWordInTree (m ,word , i +1 );
31
75
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 ();
40
76
}
41
- insert ( word , root . children [ index ], idx + 1 ) ;
77
+ return false ;
42
78
}
43
-
44
- // Returns if the word is in the trie.
45
79
public boolean search (String word ) {
46
- return search (word , root , 0 );
80
+ char a =word .charAt (0 );
81
+ if (trees [a -'a' ]==null ){
82
+ return false ;
83
+ }else {
84
+ if (1 ==word .length ()){
85
+ if (trees [a -'a' ].end ){
86
+ return true ;
87
+ }else {
88
+ return false ;
89
+ }
90
+ }
91
+ return searchWordInTree (trees [a -'a' ],word ,1 );
92
+ }
47
93
}
48
94
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 ;
95
+ public boolean startsWith (String prefix ) {
96
+ char a =prefix .charAt (0 );
97
+ ans =false ;
98
+ if (trees [a -'a' ]==null ){
57
99
return false ;
100
+ }else {
101
+ if (1 ==prefix .length ()){
102
+ return true ;
103
+ }
104
+ searchWordInTree (trees [a -'a' ],prefix ,1 );
58
105
}
59
- return search ( word , root . children [ index ], idx + 1 ) ;
106
+ return ans ;
60
107
}
61
108
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 ;
109
+ class TrieNode {
110
+ char val ;
111
+ boolean end =false ;
112
+ TrieNode [] nexts =new TrieNode [26 ];
113
+ TrieNode (char val ){
114
+ this .val =val ;
115
+ }
116
+ @ Override public String toString (){
117
+ return val +" " +nexts +" " +end ;
118
+ }
67
119
}
68
120
}
69
121
@@ -74,3 +126,11 @@ public boolean startsWith(String prefix) {
74
126
* boolean param_2 = obj.search(word);
75
127
* boolean param_3 = obj.startsWith(prefix);
76
128
*/
129
+
130
+ /*
131
+ * Your Trie object will be instantiated and called as such:
132
+ * Trie obj = new Trie();
133
+ * obj.insert(word);
134
+ * boolean param_2 = obj.search(word);
135
+ * boolean param_3 = obj.startsWith(prefix);
136
+ */
0 commit comments