Skip to content

Commit b50d7c9

Browse files
refactor 425
1 parent 86cd045 commit b50d7c9

File tree

2 files changed

+71
-68
lines changed

2 files changed

+71
-68
lines changed

src/main/java/com/fishercoder/solutions/_425.java

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -71,86 +71,90 @@ The output consists of two word squares. The order of output does not matter (ju
7171
*/
7272
public class _425 {
7373

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+
}
8387
}
84-
}
8588

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+
}
88106

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<>();
92109
TrieNode cur = root;
93-
for (char ch : word.toCharArray()) {
110+
for (char ch : prefix.toCharArray()) {
94111
int index = ch - 'a';
95112
if (cur.children[index] == null) {
96-
cur.children[index] = new TrieNode();
113+
return ans;
97114
}
98-
cur.children[index].startWith.add(word);
115+
99116
cur = cur.children[index];
100117
}
118+
ans.addAll(cur.startWith);
119+
return ans;
101120
}
102121
}
103122

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);
114135
}
115-
ans.addAll(cur.startWith);
116-
return ans;
117-
}
118-
}
119136

120-
public List<List<String>> wordSquares(String[] words) {
121-
List<List<String>> ans = new ArrayList<>();
122-
if (words == null || words.length == 0) {
123137
return ans;
124138
}
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-
}
136139

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+
}
143146

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+
}
154158
}
155159
}
156160

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
package com.fishercoder;
22

3+
import com.fishercoder.common.utils.CommonUtils;
34
import com.fishercoder.solutions._425;
45
import org.junit.BeforeClass;
56
import org.junit.Test;
67

78
import java.util.List;
89

9-
/**
10-
* Created by stevesun on 6/3/17.
11-
*/
1210
public class _425Test {
13-
private static _425 test;
11+
private static _425.Solution1 solution1;
1412
private static String[] words;
1513

1614
@BeforeClass
1715
public static void setup() {
18-
test = new _425();
16+
solution1 = new _425.Solution1();
1917
}
2018

2119
@Test
2220
public void test1() {
2321
words = new String[]{"area", "lead", "wall", "lady", "ball"};
24-
List<List<String>> result = test.wordSquares(words);
22+
List<List<String>> result = solution1.wordSquares(words);
23+
CommonUtils.printListList(result);
2524
}
2625
}

0 commit comments

Comments
 (0)