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