@@ -71,86 +71,90 @@ The output consists of two word squares. The order of output does not matter (ju
71
71
*/
72
72
public class _425 {
73
73
74
- /**Credit: https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16/2*/
75
-
76
- class TrieNode {
77
- List <String > startWith ;
78
- TrieNode [] children ;
79
-
80
- TrieNode () {
81
- startWith = new ArrayList <>();
82
- children = new TrieNode [26 ];
74
+ public static class Solution1 {
75
+ /**
76
+ * Credit: https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16/2
77
+ */
78
+
79
+ class TrieNode {
80
+ List <String > startWith ;
81
+ TrieNode [] children ;
82
+
83
+ TrieNode () {
84
+ startWith = new ArrayList <>();
85
+ children = new TrieNode [26 ];
86
+ }
83
87
}
84
- }
85
88
86
- class Trie {
87
- TrieNode root ;
89
+ class Trie {
90
+ TrieNode root ;
91
+
92
+ Trie (String [] words ) {
93
+ root = new TrieNode ();
94
+ for (String word : words ) {
95
+ TrieNode cur = root ;
96
+ for (char ch : word .toCharArray ()) {
97
+ int index = ch - 'a' ;
98
+ if (cur .children [index ] == null ) {
99
+ cur .children [index ] = new TrieNode ();
100
+ }
101
+ cur .children [index ].startWith .add (word );
102
+ cur = cur .children [index ];
103
+ }
104
+ }
105
+ }
88
106
89
- Trie (String [] words ) {
90
- root = new TrieNode ();
91
- for (String word : words ) {
107
+ List <String > findByPrefix (String prefix ) {
108
+ List <String > ans = new ArrayList <>();
92
109
TrieNode cur = root ;
93
- for (char ch : word .toCharArray ()) {
110
+ for (char ch : prefix .toCharArray ()) {
94
111
int index = ch - 'a' ;
95
112
if (cur .children [index ] == null ) {
96
- cur . children [ index ] = new TrieNode () ;
113
+ return ans ;
97
114
}
98
- cur . children [ index ]. startWith . add ( word );
115
+
99
116
cur = cur .children [index ];
100
117
}
118
+ ans .addAll (cur .startWith );
119
+ return ans ;
101
120
}
102
121
}
103
122
104
- List <String > findByPrefix (String prefix ) {
105
- List <String > ans = new ArrayList <>();
106
- TrieNode cur = root ;
107
- for (char ch : prefix .toCharArray ()) {
108
- int index = ch - 'a' ;
109
- if (cur .children [index ] == null ) {
110
- return ans ;
111
- }
112
-
113
- cur = cur .children [index ];
123
+ public List <List <String >> wordSquares (String [] words ) {
124
+ List <List <String >> ans = new ArrayList <>();
125
+ if (words == null || words .length == 0 ) {
126
+ return ans ;
127
+ }
128
+ int len = words [0 ].length ();
129
+ Trie trie = new Trie (words );
130
+ List <String > ansBuilder = new ArrayList <>();
131
+ for (String w : words ) {
132
+ ansBuilder .add (w );
133
+ search (len , trie , ans , ansBuilder );
134
+ ansBuilder .remove (ansBuilder .size () - 1 );
114
135
}
115
- ans .addAll (cur .startWith );
116
- return ans ;
117
- }
118
- }
119
136
120
- public List <List <String >> wordSquares (String [] words ) {
121
- List <List <String >> ans = new ArrayList <>();
122
- if (words == null || words .length == 0 ) {
123
137
return ans ;
124
138
}
125
- int len = words [0 ].length ();
126
- Trie trie = new Trie (words );
127
- List <String > ansBuilder = new ArrayList <>();
128
- for (String w : words ) {
129
- ansBuilder .add (w );
130
- search (len , trie , ans , ansBuilder );
131
- ansBuilder .remove (ansBuilder .size () - 1 );
132
- }
133
-
134
- return ans ;
135
- }
136
139
137
- private void search (int len , Trie trie , List <List <String >> ans ,
138
- List <String > ansBuilder ) {
139
- if (ansBuilder .size () == len ) {
140
- ans .add (new ArrayList <>(ansBuilder ));
141
- return ;
142
- }
140
+ private void search (int len , Trie trie , List <List <String >> ans ,
141
+ List <String > ansBuilder ) {
142
+ if (ansBuilder .size () == len ) {
143
+ ans .add (new ArrayList <>(ansBuilder ));
144
+ return ;
145
+ }
143
146
144
- int idx = ansBuilder .size ();
145
- StringBuilder prefixBuilder = new StringBuilder ();
146
- for (String s : ansBuilder ) {
147
- prefixBuilder .append (s .charAt (idx ));
148
- }
149
- List <String > startWith = trie .findByPrefix (prefixBuilder .toString ());
150
- for (String sw : startWith ) {
151
- ansBuilder .add (sw );
152
- search (len , trie , ans , ansBuilder );
153
- ansBuilder .remove (ansBuilder .size () - 1 );
147
+ int idx = ansBuilder .size ();
148
+ StringBuilder prefixBuilder = new StringBuilder ();
149
+ for (String s : ansBuilder ) {
150
+ prefixBuilder .append (s .charAt (idx ));
151
+ }
152
+ List <String > startWith = trie .findByPrefix (prefixBuilder .toString ());
153
+ for (String sw : startWith ) {
154
+ ansBuilder .add (sw );
155
+ search (len , trie , ans , ansBuilder );
156
+ ansBuilder .remove (ansBuilder .size () - 1 );
157
+ }
154
158
}
155
159
}
156
160
0 commit comments