Skip to content

Commit 51f491f

Browse files
authored
Improved task 211.
1 parent 02b5c9e commit 51f491f

File tree

2 files changed

+38
-49
lines changed

2 files changed

+38
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3592,7 +3592,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.17'
35923592
| 0214 |[Shortest Palindrome](src/main/java/g0201_0300/s0214_shortest_palindrome/Solution.java)| Hard | String, Hash_Function, String_Matching, Rolling_Hash | 3 | 96.59
35933593
| 0213 |[House Robber II](src/main/java/g0201_0300/s0213_house_robber_ii/Solution.java)| Medium | Array, Dynamic_Programming, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Udemy_Dynamic_Programming | 0 | 100.00
35943594
| 0212 |[Word Search II](src/main/java/g0201_0300/s0212_word_search_ii/Solution.java)| Hard | Top_Interview_Questions, Array, String, Matrix, Backtracking, Trie | 21 | 99.42
3595-
| 0211 |[Design Add and Search Words Data Structure](src/main/java/g0201_0300/s0211_design_add_and_search_words_data_structure/WordDictionary.java)| Medium | String, Depth_First_Search, Design, Trie | 445 | 96.00
3595+
| 0211 |[Design Add and Search Words Data Structure](src/main/java/g0201_0300/s0211_design_add_and_search_words_data_structure/WordDictionary.java)| Medium | String, Depth_First_Search, Design, Trie | 308 | 99.46
35963596
| 0210 |[Course Schedule II](src/main/java/g0201_0300/s0210_course_schedule_ii/Solution.java)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Level_2_Day_11_Graph/BFS/DFS | 13 | 35.17
35973597
| 0209 |[Minimum Size Subarray Sum](src/main/java/g0201_0300/s0209_minimum_size_subarray_sum/Solution.java)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Binary_Search_II_Day_1 | 1 | 100.00
35983598
| 0208 |[Implement Trie (Prefix Tree)](src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/Trie.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap | 34 | 99.90

src/main/java/g0201_0300/s0211_design_add_and_search_words_data_structure/WordDictionary.java

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,67 @@
11
package g0201_0300.s0211_design_add_and_search_words_data_structure;
22

33
// #Medium #String #Depth_First_Search #Design #Trie
4-
// #2022_07_02_Time_445_ms_(96.00%)_Space_104.3_MB_(83.47%)
4+
// #2023_01_06_Time_308_ms_(99.46%)_Space_284.7_MB_(13.25%)
55

66
public class WordDictionary {
77

8-
private static class Node {
9-
char value;
10-
boolean isEnd;
11-
Node[] childs;
12-
13-
Node(char value) {
14-
this.value = value;
15-
this.isEnd = false;
16-
this.childs = new Node[26];
17-
}
18-
19-
Node getChild(char ch) {
20-
return this.childs[ch - 'a'];
21-
}
22-
23-
boolean isChild(char ch) {
24-
return getChild(ch) != null;
25-
}
8+
public WordDictionary() {}
269

27-
void addChild(char ch) {
28-
this.childs[ch - 'a'] = new Node(ch);
29-
}
10+
private static class Node {
11+
Node[] kids = new Node[26];
12+
boolean isTerminal;
3013
}
31-
// dummy value
32-
private Node root = new Node('a');
3314

34-
public WordDictionary() {
35-
// empty constructor
36-
}
15+
private final Node[] root = new Node[26];
3716

3817
public void addWord(String word) {
39-
Node node = root;
40-
for (int i = 0; i < word.length(); i++) {
41-
char ch = word.charAt(i);
42-
if (!node.isChild(ch)) {
43-
node.addChild(ch);
18+
int n = word.length();
19+
if (root[n] == null) {
20+
root[n] = new Node();
21+
}
22+
Node node = root[n];
23+
for (int i = 0; i < n; i++) {
24+
int c = word.charAt(i) - 'a';
25+
Node kid = node.kids[c];
26+
if (kid == null) {
27+
kid = new Node();
28+
node.kids[c] = kid;
4429
}
45-
node = node.getChild(ch);
30+
node = kid;
4631
}
47-
node.isEnd = true;
32+
node.isTerminal = true;
4833
}
4934

5035
public boolean search(String word) {
51-
return dfs(this.root, word, 0);
36+
Node node = root[word.length()];
37+
return node != null && dfs(0, node, word);
5238
}
5339

54-
public boolean dfs(Node root, String word, int index) {
55-
if (root == null) {
40+
private boolean dfs(int i, Node node, String word) {
41+
int len = word.length();
42+
if (i == len) {
5643
return false;
5744
}
58-
// if reached end of word
59-
if (index == word.length()) {
60-
return root.isEnd;
61-
}
62-
char ch = word.charAt(index);
63-
if (ch == '.') {
64-
for (Node child : root.childs) {
65-
boolean found = dfs(child, word, index + 1);
66-
if (found) {
45+
char c = word.charAt(i);
46+
if (c == '.') {
47+
for (Node kid : node.kids) {
48+
if (kid == null) {
49+
continue;
50+
}
51+
if (i == len - 1 && kid.isTerminal || dfs(i + 1, kid, word)) {
6752
return true;
6853
}
6954
}
7055
return false;
7156
}
72-
if (!root.isChild(ch)) {
57+
Node kid = node.kids[c - 'a'];
58+
if (kid == null) {
7359
return false;
7460
}
75-
return dfs(root.getChild(ch), word, index + 1);
61+
if (i == len - 1) {
62+
return kid.isTerminal;
63+
}
64+
return dfs(i + 1, kid, word);
7665
}
7766
}
7867

0 commit comments

Comments
 (0)