Skip to content

Commit 510fd04

Browse files
committed
✨ [211] FIRST TRIE!!!
1 parent 13c30ad commit 510fd04

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed

211/my_solution.js

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
class TrieNode {
2+
3+
constructor() {
4+
const ALPHABET_LENGTH = 26;
5+
6+
this.isEndOfWord = false;
7+
this.children = new Array(ALPHABET_LENGTH).fill(null)
8+
}
9+
10+
getIdx(char) {
11+
return char.charCodeAt() - "a".charCodeAt();
12+
}
13+
}
14+
15+
16+
class WordDictionary {
17+
constructor() {
18+
this.root = new TrieNode();
19+
}
20+
21+
/**
22+
* @param {string} word
23+
* @return {void}
24+
*/
25+
addWord(word) {
26+
27+
let root = this.root;
28+
for (let i = 0; i < word.length; i++) {
29+
// console.log(word[i].charCodeAt() - "a".charCodeAt())
30+
let idx = root.getIdx(word[i]);
31+
if (null === root.children[idx]) root.children[idx] = new TrieNode();
32+
root = root.children[idx];
33+
}
34+
35+
root.isEndOfWord = true;
36+
// console.log(this.root)
37+
// console.log(this.root.children[3])
38+
// console.log(this.root.children[3].children[0])
39+
// console.log(this.root.children[3].children[0].children)
40+
}
41+
42+
/**
43+
* @param {string} word
44+
* @return {boolean}
45+
*/
46+
search(word) {
47+
const dfs = (i, node) => {
48+
console.log(`word:${word}, i:${i}, l:${word.length}`)
49+
if (node.isEndOfWord && i === word.length) return true;
50+
if (word[i] === undefined) return false;
51+
let idx = node.getIdx(word[i]), nextI = i + 1;
52+
53+
console.log(`node.getIdx(word[i]): ${node.getIdx(word[i])}`)
54+
55+
if (idx < 0) {
56+
console.log("word[i]")
57+
console.log(word[i])
58+
// console.log(node.children)
59+
60+
for (let possibleChild of node.children) {
61+
if (possibleChild === null) continue;
62+
63+
console.log("possibleChild")
64+
console.log(possibleChild)
65+
if (dfs(nextI, possibleChild)) return true;
66+
}
67+
console.log("??????")
68+
// all null
69+
return false;
70+
} else {
71+
let child = node.children[idx];
72+
console.log("child")
73+
console.log(child)
74+
75+
if (null === child) return false;
76+
77+
return dfs(nextI, child);
78+
}
79+
}
80+
81+
return dfs(0, this.root);
82+
}
83+
};
84+
85+
// let tn = new TrieNode();
86+
// tn.insert("dad")
87+
// tn.insert("daddy")
88+
// console.log(tn.children[3])
89+
// console.log(tn.children[3].children[0])
90+
let wd = new WordDictionary();
91+
92+
wd.addWord("at")
93+
wd.addWord("and")
94+
wd.addWord("an")
95+
wd.addWord("add")
96+
console.log(wd.search("a"))
97+
console.log(wd.search(".at"))
98+
wd.addWord("bat")
99+
console.log(wd.search(".at"))
100+
101+
// for (word of [".at", "an.", "a.d.", "b.", "a.d", "."]) {
102+
// console.log(wd.search(word))
103+
//
104+
// console.log("word")
105+
// console.log(word)
106+
// }
107+
// wd.addWord("a")
108+
// wd.addWord("ab")
109+
// console.log(wd.root.children)
110+
111+
112+
113+
// console.log(wd.search("a"))
114+
// console.log(wd.search("a."))
115+
// console.log(wd.search("ab"))
116+
// console.log(wd.search(".a"))
117+
// console.log(wd.search(".b"))
118+
// console.log(wd.search("ab."))
119+
// console.log(wd.search("."))
120+
// console.log(wd.search(".."))
121+
122+
// wd.addWord("a")
123+
// wd.addWord("a")
124+
125+
// wd.addWord("dad")
126+
// wd.addWord("mad")
127+
// wd.addWord("pad")
128+
129+
// console.log("Result")
130+
// console.log(wd.search(".")) // t
131+
// console.log(wd.search("a")) // t
132+
// console.log(wd.search("aa"))
133+
// console.log(wd.search("a")) // t
134+
// console.log(wd.search(".a"))
135+
// console.log(wd.search("a."))
136+
// console.log(wd.search("a"))
137+
138+
// [
139+
// "WordDictionary",
140+
// "addWord", "addWord", "addWord", "addWord",
141+
// "search", "search",
142+
// "addWord",
143+
// "search", "search", "search", "search", "search", "search"
144+
// ]
145+
// [
146+
// [],
147+
// ["at"], ["and"], ["an"], ["add"],
148+
// ["a"], [".at"],
149+
// ["bat"],
150+
// [".at"], ["an."], ["a.d."], ["b."], ["a.d"], ["."]
151+
// ]

211/solution.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class TrieNode {
2+
3+
constructor() {
4+
const ALPHABET_LENGTH = 26;
5+
6+
this.isEndOfWord = false;
7+
this.children = new Array(ALPHABET_LENGTH).fill(null)
8+
}
9+
10+
getIdx(char) {
11+
return char.charCodeAt() - "a".charCodeAt();
12+
}
13+
}
14+
15+
16+
class WordDictionary {
17+
constructor() {
18+
this.root = new TrieNode();
19+
}
20+
21+
/**
22+
* @param {string} word
23+
* @return {void}
24+
*/
25+
addWord(word) {
26+
27+
let root = this.root;
28+
for (let i = 0; i < word.length; i++) {
29+
let idx = root.getIdx(word[i]);
30+
if (null === root.children[idx]) root.children[idx] = new TrieNode();
31+
root = root.children[idx];
32+
}
33+
34+
root.isEndOfWord = true;
35+
}
36+
37+
/**
38+
* @param {string} word
39+
* @return {boolean}
40+
*/
41+
search(word) {
42+
const dfs = (i, node) => {
43+
if (node.isEndOfWord && i === word.length) return true;
44+
if (word[i] === undefined) return false;
45+
let idx = node.getIdx(word[i]), nextI = i + 1;
46+
47+
if (idx < 0) {
48+
for (let possibleChild of node.children) {
49+
if (possibleChild === null) continue;
50+
if (dfs(nextI, possibleChild)) return true;
51+
}
52+
53+
return false;
54+
} else {
55+
let child = node.children[idx];
56+
57+
if (null === child) return false;
58+
59+
return dfs(nextI, child);
60+
}
61+
}
62+
63+
return dfs(0, this.root);
64+
}
65+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
- [200. Number of Islands](./200/)
5757
- [206. Reverse Linked List](./206/)
5858
- [207. Course Schedule](./207/)
59+
- [211. Design Add and Search Words Data Structure](./211/)
5960
- [213. House Robber II](./213/)
6061
- [217. Contains Duplicate](./217/)
6162
- [226. Invert Binary Tree](./226/)
@@ -146,7 +147,7 @@ Batch create:
146147
NOTE: JS IS HERE
147148
-->
148149
```ssh
149-
chapter=230 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
150+
chapter=211 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
150151
```
151152
> then you can use `x` for quick debug.
152153

0 commit comments

Comments
 (0)