1
1
package com .fishercoder .solutions ;
2
2
3
3
/**
4
+ * 211. Add and Search Word - Data structure design
5
+ *
4
6
* Design a data structure that supports the following two operations:
5
7
6
8
void addWord(word)
@@ -22,74 +24,80 @@ bool search(word)
22
24
You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
23
25
*/
24
26
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 ;
27
30
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
+ }
32
35
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
+ }
47
40
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 ;
50
52
}
53
+ addWord (chars , ++index , node );
54
+ }
51
55
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
+ }
77
78
}
78
- return search (chars , ++index , node );
79
+ }
80
+ return false ;
79
81
}
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 ;
85
85
}
86
+ return search (chars , ++index , node );
87
+ }
86
88
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
+ }
87
94
}
88
95
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
+ }
95
103
}
0 commit comments