55// #Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N)
66// #2024_11_15_Time_30_ms_(99.78%)_Space_55.1_MB_(72.51%)
77
8+ import java .util .Arrays ;
9+
810@ SuppressWarnings ("java:S1104" )
911public class Trie {
10- boolean ans =false ;
11- TrieNode [] trees =new TrieNode [26 ];
12- public Trie () {
13-
14- }
12+ private boolean ans = false ;
13+ private final TrieNode [] trees = new TrieNode [26 ];
14+ public Trie () {}
1515
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 ;
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+ } else {
24+ a .end = true ;
2825 }
26+ found = true ;
2927 }
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 ;
28+ if (!found ) {
29+ TrieNode prev = t ;
30+ for (int j = i ; j < word .length (); j ++) {
31+ TrieNode temp = new TrieNode (word .charAt (j ));
32+ prev .nexts [word .charAt (j ) - 'a' ] = temp ;
33+ prev = temp ;
3634 }
37- prev .end = true ;
35+ prev .end = true ;
3836 }
3937 return t ;
4038 }
4139
4240 public void insert (String word ) {
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 );
41+ char a = word .charAt (0 );
42+ if (trees [a - 'a' ] == null ) {
43+ TrieNode t = new TrieNode (a );
44+ trees [a - 'a' ] = t ;
45+ }
46+ if (1 == word .length ()) {
47+ trees [a - 'a' ].end = true ;
48+ return ;
5849 }
59- //System.out.println (trees[a- 'a']);
50+ trees [ a - 'a' ] = mapWordToTree (trees [a - 'a' ], word , 1 );
6051 }
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 ){
6552
66- if ( i == word . length ()- 1 ) {
67- ans = true ;
68- if ( m . end ){
69- return true ;
70- } else {
71- return false ;
72- }
53+ public boolean searchWordInTree ( TrieNode t , String word , int i ) {
54+ char a = word . charAt ( i ) ;
55+ TrieNode m = t . nexts [ a - 'a' ];
56+ if ( m != null ) {
57+ if ( i == word . length () - 1 ) {
58+ ans = true ;
59+ return m . end ;
7360 }
74- return searchWordInTree (m ,word , i +1 );
75-
61+ return searchWordInTree (m , word , i + 1 );
7662 }
7763 return false ;
7864 }
65+
7966 public boolean search (String word ) {
80- char a = word .charAt (0 );
81- if (trees [a - 'a' ]== null ){
67+ char a = word .charAt (0 );
68+ if (trees [a - 'a' ] == null ) {
8269 return false ;
83- }else {
84- if (1 ==word .length ()){
85- if (trees [a -'a' ].end ){
86- return true ;
87- }else {
88- return false ;
89- }
70+ } else {
71+ if (1 == word .length ()) {
72+ return trees [a - 'a' ].end ;
9073 }
91- return searchWordInTree (trees [a - 'a' ],word ,1 );
74+ return searchWordInTree (trees [a - 'a' ], word , 1 );
9275 }
9376 }
9477
9578 public boolean startsWith (String prefix ) {
96- char a = prefix .charAt (0 );
97- ans = false ;
98- if (trees [a - 'a' ]== null ){
79+ char a = prefix .charAt (0 );
80+ ans = false ;
81+ if (trees [a - 'a' ] == null ) {
9982 return false ;
100- }else {
101- if ( 1 == prefix .length ()){
83+ } else {
84+ if ( 1 == prefix .length ()) {
10285 return true ;
10386 }
104- searchWordInTree (trees [a - 'a' ],prefix ,1 );
87+ searchWordInTree (trees [a - 'a' ], prefix , 1 );
10588 }
10689 return ans ;
10790 }
10891
109- class TrieNode {
92+ static class TrieNode {
11093 char val ;
111- boolean end =false ;
112- TrieNode [] nexts =new TrieNode [26 ];
113- TrieNode (char val ){
114- this .val =val ;
94+ boolean end = false ;
95+ TrieNode [] nexts = new TrieNode [26 ];
96+
97+ TrieNode (char val ) {
98+ this .val = val ;
11599 }
116- @ Override public String toString (){
117- return val +" " +nexts +" " +end ;
100+
101+ @ Override
102+ public String toString () {
103+ return val + " " + Arrays .toString (nexts ) + " " + end ;
118104 }
119105 }
120106}
@@ -126,11 +112,3 @@ class TrieNode{
126112 * boolean param_2 = obj.search(word);
127113 * boolean param_3 = obj.startsWith(prefix);
128114 */
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