|
| 1 | +/* |
| 2 | +https://leetcode.com/problems/design-add-and-search-words-data-structure |
| 3 | +
|
| 4 | +Design a data structure that supports adding new words and searching for existing words. |
| 5 | +
|
| 6 | +Implement the WordDictionary class: |
| 7 | +
|
| 8 | +void addWord(word) Adds word to the data structure. |
| 9 | +bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter. |
| 10 | +
|
| 11 | +Example 1: |
| 12 | +Input: |
| 13 | +["WordDictionary", "addWord", "day", "addWord", "bay", "addWord", "may", "search", "say", "search", "day", "search", ".ay", "search", "b.."] |
| 14 | +Output: |
| 15 | +[null, null, null, null, false, true, true, true] |
| 16 | +
|
| 17 | +Explanation: |
| 18 | +WordDictionary wordDictionary = new WordDictionary(); |
| 19 | +wordDictionary.addWord("day"); |
| 20 | +wordDictionary.addWord("bay"); |
| 21 | +wordDictionary.addWord("may"); |
| 22 | +wordDictionary.search("say"); // return false |
| 23 | +wordDictionary.search("day"); // return true |
| 24 | +wordDictionary.search(".ay"); // return true |
| 25 | +wordDictionary.search("b.."); // return true |
| 26 | +
|
| 27 | +Constraints: |
| 28 | +1 <= word.length <= 20 |
| 29 | +word in addWord consists of lowercase English letters. |
| 30 | +word in search consist of '.' or lowercase English letters. |
| 31 | +There will be at most 2 dots in word for search queries. |
| 32 | +At most 10,000 calls will be made to addWord and search. |
| 33 | +*/ |
| 34 | + |
| 35 | +class TrieNode { |
| 36 | + constructor(val) { |
| 37 | + this.val = val; |
| 38 | + this.isWord = false; |
| 39 | + this.children = {}; |
| 40 | + } |
| 41 | + |
| 42 | + setAsWord() { |
| 43 | + this.isWord = true; |
| 44 | + } |
| 45 | + |
| 46 | + setChild(node) { |
| 47 | + this.children[node.val] = node; |
| 48 | + } |
| 49 | + |
| 50 | + getChild(val) { |
| 51 | + return this.children[val]; |
| 52 | + } |
| 53 | + |
| 54 | + getChildren() { |
| 55 | + return this.children; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class WordDictionary { |
| 60 | + constructor() { |
| 61 | + this.root = new TrieNode("__root__"); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param {string} word |
| 66 | + * @return {void} |
| 67 | + */ |
| 68 | + addWord(word) { |
| 69 | + let currentNode = this.root; |
| 70 | + for (let i = 0; i < word.length; i++) { |
| 71 | + const char = word[i]; |
| 72 | + |
| 73 | + if (!currentNode.getChild(char)) { |
| 74 | + currentNode.setChild(new TrieNode(char)); |
| 75 | + } |
| 76 | + currentNode = currentNode.getChild(char); |
| 77 | + |
| 78 | + if (i === word.length - 1) { |
| 79 | + currentNode.setAsWord(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param {string} word |
| 86 | + * @return {boolean} |
| 87 | + */ |
| 88 | + search(word) { |
| 89 | + function bfs(node, i) { |
| 90 | + if (i === word.length && node.isWord) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + const nodeChildren = node.getChildren(); |
| 95 | + if (!Object.keys(nodeChildren).length) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + const char = word[i]; |
| 100 | + const results = []; |
| 101 | + if (char === ".") { |
| 102 | + for (const child of Object.keys(nodeChildren)) { |
| 103 | + results.push(bfs(nodeChildren[child], i + 1)); |
| 104 | + } |
| 105 | + } else if (nodeChildren[char]) { |
| 106 | + results.push(bfs(nodeChildren[char], i + 1)); |
| 107 | + } |
| 108 | + |
| 109 | + return results.some((result) => result); |
| 110 | + } |
| 111 | + |
| 112 | + return bfs(this.root, 0); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +module.exports = { TrieNode, WordDictionary }; |
0 commit comments