Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7acaafe

Browse files
refactor 647
1 parent f21b148 commit 7acaafe

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

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

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,64 @@
2929
3030
*/
3131
public class _648 {
32-
public String replaceWords(List<String> dict, String sentence) {
33-
String[] tokens = sentence.split(" ");
34-
TrieNode trie = buildTrie(dict);
35-
return replaceWords(tokens, trie);
36-
}
32+
public static class Solution1 {
33+
public String replaceWords(List<String> dict, String sentence) {
34+
String[] tokens = sentence.split(" ");
35+
TrieNode trie = buildTrie(dict);
36+
return replaceWords(tokens, trie);
37+
}
3738

38-
private String replaceWords(String[] tokens, TrieNode root) {
39-
StringBuilder stringBuilder = new StringBuilder();
40-
for (String token : tokens) {
41-
stringBuilder.append(getShortestReplacement(token, root));
42-
stringBuilder.append(" ");
39+
private String replaceWords(String[] tokens, TrieNode root) {
40+
StringBuilder stringBuilder = new StringBuilder();
41+
for (String token : tokens) {
42+
stringBuilder.append(getShortestReplacement(token, root));
43+
stringBuilder.append(" ");
44+
}
45+
return stringBuilder.substring(0, stringBuilder.length() - 1);
4346
}
44-
return stringBuilder.substring(0, stringBuilder.length() - 1);
45-
}
4647

47-
private String getShortestReplacement(String token, final TrieNode root) {
48-
TrieNode temp = root;
49-
StringBuilder stringBuilder = new StringBuilder();
50-
for (char c : token.toCharArray()) {
51-
stringBuilder.append(c);
52-
if (temp.children[c - 'a'] != null) {
53-
if (temp.children[c - 'a'].isWord) {
54-
return stringBuilder.toString();
48+
private String getShortestReplacement(String token, final TrieNode root) {
49+
TrieNode temp = root;
50+
StringBuilder stringBuilder = new StringBuilder();
51+
for (char c : token.toCharArray()) {
52+
stringBuilder.append(c);
53+
if (temp.children[c - 'a'] != null) {
54+
if (temp.children[c - 'a'].isWord) {
55+
return stringBuilder.toString();
56+
}
57+
temp = temp.children[c - 'a'];
58+
} else {
59+
return token;
5560
}
56-
temp = temp.children[c - 'a'];
57-
} else {
58-
return token;
5961
}
62+
return token;
6063
}
61-
return token;
62-
}
6364

64-
private TrieNode buildTrie(List<String> dict) {
65-
TrieNode root = new TrieNode(' ');
66-
for (String word : dict) {
67-
TrieNode temp = root;
68-
for (char c : word.toCharArray()) {
69-
if (temp.children[c - 'a'] == null) {
70-
temp.children[c - 'a'] = new TrieNode(c);
65+
private TrieNode buildTrie(List<String> dict) {
66+
TrieNode root = new TrieNode(' ');
67+
for (String word : dict) {
68+
TrieNode temp = root;
69+
for (char c : word.toCharArray()) {
70+
if (temp.children[c - 'a'] == null) {
71+
temp.children[c - 'a'] = new TrieNode(c);
72+
}
73+
temp = temp.children[c - 'a'];
7174
}
72-
temp = temp.children[c - 'a'];
75+
temp.isWord = true;
7376
}
74-
temp.isWord = true;
77+
return root;
7578
}
76-
return root;
77-
}
7879

79-
public class TrieNode {
80-
char val;
81-
TrieNode[] children;
82-
boolean isWord;
80+
public class TrieNode {
81+
char val;
82+
TrieNode[] children;
83+
boolean isWord;
8384

84-
public TrieNode(char val) {
85-
this.val = val;
86-
this.children = new TrieNode[26];
87-
this.isWord = false;
85+
public TrieNode(char val) {
86+
this.val = val;
87+
this.children = new TrieNode[26];
88+
this.isWord = false;
89+
}
8890
}
8991
}
9092
}

src/test/java/com/fishercoder/_648Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
import static org.junit.Assert.assertEquals;
1111

1212
public class _648Test {
13-
private static _648 test;
13+
private static _648.Solution1 solution1;
1414
private static List<String> dict;
1515
private static String sentence;
1616

1717
@BeforeClass
1818
public static void setup() {
19-
test = new _648();
19+
solution1 = new _648.Solution1();
2020
}
2121

2222
@Test
2323
public void test1() {
2424
dict = Arrays.asList("cat", "bat", "rat");
2525
sentence = "the cattle was rattled by the battery";
26-
assertEquals("the cat was rat by the bat", test.replaceWords(dict, sentence));
26+
assertEquals("the cat was rat by the bat", solution1.replaceWords(dict, sentence));
2727
}
2828

2929
@Test
@@ -57,7 +57,7 @@ public void test2() {
5757
+ "dijhrrpnwjlju muzzrrsypzgwvblf z h q i daee r nlipyfszvxlwqw "
5858
+ "yoq dewpgtcrzausqwhh q i k bqprarpgnyemzwifqzz "
5959
+ "oai pnqottd nygesjtlpala q gyvukjpc s mxhlkdaycskj "
60-
+ "uvwmerplaibeknltuvd ocnn f c pxbd oklwhcppuziixpvihihp", test.replaceWords(dict, sentence));
60+
+ "uvwmerplaibeknltuvd ocnn f c pxbd oklwhcppuziixpvihihp", solution1.replaceWords(dict, sentence));
6161
}
6262

6363
}

0 commit comments

Comments
 (0)