|
| 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 | +// ] |
0 commit comments