Skip to content

Commit 11c6492

Browse files
refactor 211
1 parent bb01f36 commit 11c6492

File tree

2 files changed

+94
-59
lines changed

2 files changed

+94
-59
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_211.java

+67-59
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 211. Add and Search Word - Data structure design
5+
*
46
* Design a data structure that supports the following two operations:
57
68
void addWord(word)
@@ -22,74 +24,80 @@ bool search(word)
2224
You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
2325
*/
2426
public class _211 {
25-
public class WordDictionary {
26-
WordNode root = new WordNode();
27+
public static class Solution1 {
28+
public static class WordDictionary {
29+
WordNode root;
2730

28-
public void addWord(String word) {
29-
char[] chars = word.toCharArray();
30-
addWord(chars, 0, root);
31-
}
31+
/** Initialize your data structure here. */
32+
public WordDictionary() {
33+
root = new WordNode();
34+
}
3235

33-
private void addWord(char[] chars, int index, WordNode parent) {
34-
char c = chars[index];
35-
int idx = c - 'a';
36-
WordNode node = parent.children[idx];
37-
if (node == null) {
38-
node = new WordNode();
39-
parent.children[idx] = node;
40-
}
41-
if (chars.length == index + 1) {
42-
node.isLeaf = true;
43-
return;
44-
}
45-
addWord(chars, ++index, node);
46-
}
36+
public void addWord(String word) {
37+
char[] chars = word.toCharArray();
38+
addWord(chars, 0, root);
39+
}
4740

48-
public boolean search(String word) {
49-
return search(word.toCharArray(), 0, root);
41+
private void addWord(char[] chars, int index, WordNode parent) {
42+
char c = chars[index];
43+
int idx = c - 'a';
44+
WordNode node = parent.children[idx];
45+
if (node == null) {
46+
node = new WordNode();
47+
parent.children[idx] = node;
48+
}
49+
if (chars.length == index + 1) {
50+
node.isLeaf = true;
51+
return;
5052
}
53+
addWord(chars, ++index, node);
54+
}
5155

52-
/**This is also a beautifully designed recursive function.*/
53-
private boolean search(char[] chars, int index, WordNode parent) {
54-
if (index == chars.length) {
55-
if (parent.isLeaf) {
56-
return true;
57-
}
58-
return false;
59-
}
60-
WordNode[] childNodes = parent.children;
61-
char c = chars[index];
62-
if (c == '.') {
63-
for (int i = 0; i < childNodes.length; i++) {
64-
WordNode n = childNodes[i];
65-
if (n != null) {
66-
boolean b = search(chars, index + 1, n);
67-
if (b) {
68-
return true;
69-
}
70-
}
71-
}
72-
return false;
73-
}
74-
WordNode node = childNodes[c - 'a'];
75-
if (node == null) {
76-
return false;
56+
public boolean search(String word) {
57+
return search(word.toCharArray(), 0, root);
58+
}
59+
60+
/** This is also a beautifully designed recursive function. */
61+
private boolean search(char[] chars, int index, WordNode parent) {
62+
if (index == chars.length) {
63+
if (parent.isLeaf) {
64+
return true;
65+
}
66+
return false;
67+
}
68+
WordNode[] childNodes = parent.children;
69+
char c = chars[index];
70+
if (c == '.') {
71+
for (int i = 0; i < childNodes.length; i++) {
72+
WordNode n = childNodes[i];
73+
if (n != null) {
74+
boolean b = search(chars, index + 1, n);
75+
if (b) {
76+
return true;
77+
}
7778
}
78-
return search(chars, ++index, node);
79+
}
80+
return false;
7981
}
80-
81-
/**This is a cool/standard design for a Trie node class.*/
82-
private class WordNode {
83-
boolean isLeaf;
84-
WordNode[] children = new WordNode[26];
82+
WordNode node = childNodes[c - 'a'];
83+
if (node == null) {
84+
return false;
8585
}
86+
return search(chars, ++index, node);
87+
}
8688

89+
/** This is a cool/standard design for a Trie node class. */
90+
private class WordNode {
91+
boolean isLeaf;
92+
WordNode[] children = new WordNode[26];
93+
}
8794
}
8895

89-
/**
90-
* Your WordDictionary object will be instantiated and called as such:
91-
* WordDictionary obj = new WordDictionary();
92-
* obj.addWord(word);
93-
* boolean param_2 = obj.search(word);
94-
*/
96+
/**
97+
* Your WordDictionary object will be instantiated and called as such:
98+
* WordDictionary obj = new WordDictionary();
99+
* obj.addWord(word);
100+
* boolean param_2 = obj.search(word);
101+
*/
102+
}
95103
}

Diff for: src/test/java/com/fishercoder/_211Test.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._211;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _211Test {
10+
private static _211.Solution1.WordDictionary wordDictionarySolution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
wordDictionarySolution1 = new _211.Solution1.WordDictionary();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
wordDictionarySolution1.addWord("bad");
20+
wordDictionarySolution1.addWord("dad");
21+
wordDictionarySolution1.addWord("mad");
22+
assertEquals(false, wordDictionarySolution1.search("pad"));
23+
assertEquals(true, wordDictionarySolution1.search("bad"));
24+
assertEquals(true, wordDictionarySolution1.search(".ad"));
25+
assertEquals(true, wordDictionarySolution1.search("b.."));
26+
}
27+
}

0 commit comments

Comments
 (0)