|
| 1 | +/** |
| 2 | + * ๋จ์ด๋ฅผ ์ ์ฅํ๊ณ ๊ฒ์ํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ ๋ง๋ค๊ธฐ |
| 3 | + * |
| 4 | + * ํธ๋ผ์ด(Trie) ์๋ฃ๊ตฌ์กฐ: ๋ฌธ์์ด ์งํฉ์ ํํํ๋ ํธ๋ฆฌ ๊ธฐ๋ฐ ์๋ฃ๊ตฌ์กฐ, (๋ฌธ์์ด ๊ฒ์์ ํจ์จ์ ) |
| 5 | + - ๊ฐ ๋
ธ๋๋ ๋ฌธ์ ํ๋๋ฅผ ๋ํ๋ |
| 6 | + - ๋ฃจํธ์์ ํน์ ๋
ธ๋๊น์ง์ ๊ฒฝ๋ก๋ ํ๋์ ๋ฌธ์์ด์ ๋ํ๋ |
| 7 | + - ๊ฐ ๋
ธ๋๋ ์์ ๋
ธ๋๋ค์ ๊ฐ๋ฆฌํค๋ ๋งํฌ(๋ณดํต ํด์๋งต)๋ฅผ ๊ฐ์ง |
| 8 | + - ๋จ์ด์ ๋์ ํ์ํ๋ ํ๋๊ทธ๊ฐ ํ์ํจ |
| 9 | + * |
| 10 | + * ๋๋ถ๋ถ์ ๊ฒ์์ ์์ผ๋์นด๋๊ฐ ์๋ ๊ฒฝ์ฐ์ผ ํ
๋๊น, ์ด๊ฒ๋ง ์ฒ๋ฆฌํ๋ ๋ณ๋ ๋ฉ์๋๋ก ๋ฝ์(์์ผ๋์นด๋ ๋ถ๊ธฐ์ฒ๋ฆฌ) |
| 11 | + * |
| 12 | + * ์๊ฐ๋ณต์ก๋: O(m) (m: ๋จ์ด์ ๊ธธ์ด) |
| 13 | + * ๊ณต๊ฐ๋ณต์ก๋: O(n) (n: ๋จ์ด์ ๊ฐ์) |
| 14 | + */ |
| 15 | + |
| 16 | +var WordDictionary = function () { |
| 17 | + // ํธ๋ผ์ด ๋
ธ๋ ํด๋์ค ์ ์ |
| 18 | + this.TrieNode = function () { |
| 19 | + this.children = {}; // ์์ ๋
ธ๋๋ค์ ์ ์ฅํ๋ ํด์๋งต |
| 20 | + this.isEnd = false; // ๋จ์ด์ ๋์ ํ์ํ๋ ํ๋๊ทธ |
| 21 | + }; |
| 22 | + |
| 23 | + this.root = new this.TrieNode(); // ๋ฃจํธ ๋
ธ๋ ์์ฑ |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * ๋จ์ด๋ฅผ ํธ๋ผ์ด์ ์ถ๊ฐ |
| 28 | + * @param {string} word |
| 29 | + * @return {void} |
| 30 | + */ |
| 31 | +WordDictionary.prototype.addWord = function (word) { |
| 32 | + let node = this.root; |
| 33 | + |
| 34 | + // ๋จ์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ํํ๋ฉฐ ํธ๋ผ์ด์ ์ถ๊ฐ |
| 35 | + for (let i = 0; i < word.length; i++) { |
| 36 | + const char = word[i]; |
| 37 | + |
| 38 | + // ํ์ฌ ๋ฌธ์์ ํด๋นํ๋ ์์ ๋
ธ๋๊ฐ ์์ผ๋ฉด ์์ฑ |
| 39 | + if (!node.children[char]) { |
| 40 | + node.children[char] = new this.TrieNode(); |
| 41 | + } |
| 42 | + |
| 43 | + // ๋ค์ ๋ ๋ฒจ๋ก ์ด๋ |
| 44 | + node = node.children[char]; |
| 45 | + } |
| 46 | + |
| 47 | + // ๋จ์ด์ ๋ ํ์ |
| 48 | + node.isEnd = true; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * ํธ๋ผ์ด์์ ๋จ์ด ๊ฒ์ (์์ผ๋์นด๋ '.' ์ง์) |
| 53 | + * @param {string} word |
| 54 | + * @return {boolean} |
| 55 | + */ |
| 56 | +WordDictionary.prototype.search = function (word) { |
| 57 | + // ์์ผ๋์นด๋๊ฐ ์๋ ๊ฒฝ์ฐ |
| 58 | + if (!word.includes('.')) { |
| 59 | + return this.searchExact(word); |
| 60 | + } |
| 61 | + |
| 62 | + return this.searchWithWildcard(word, 0, this.root); |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * ์์ผ๋์นด๋ ์์ด ์ ํํ ๋จ์ด ๊ฒ์ |
| 67 | + * @param {string} word |
| 68 | + * @return {boolean} |
| 69 | + */ |
| 70 | +WordDictionary.prototype.searchExact = function (word) { |
| 71 | + let node = this.root; |
| 72 | + |
| 73 | + for (let i = 0; i < word.length; i++) { |
| 74 | + const char = word[i]; |
| 75 | + |
| 76 | + // ํด๋น ๋ฌธ์์ ์์ ๋
ธ๋๊ฐ ์์ผ๋ฉด false |
| 77 | + if (!node.children[char]) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + node = node.children[char]; |
| 82 | + } |
| 83 | + |
| 84 | + // ๋จ์ด์ ๋์ ๋๋ฌํ์ ๋ isEnd ํ๋๊ทธ ํ์ธ |
| 85 | + return node.isEnd; |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * ์์ผ๋์นด๋๋ฅผ ํฌํจํ ๋จ์ด ๊ฒ์ (์ฌ๊ท์ ) |
| 90 | + * @param {string} word - ๊ฒ์ํ ๋จ์ด |
| 91 | + * @param {number} index - ํ์ฌ ๊ฒ์์ค์ธ ๋ฌธ์ ์ธ๋ฑ์ค |
| 92 | + * @param {object} node - ํ์ฌ ๊ฒ์์ค์ธ ๋
ธ๋ |
| 93 | + * @return {boolean} |
| 94 | + */ |
| 95 | +WordDictionary.prototype.searchWithWildcard = function (word, index, node) { |
| 96 | + // ๋จ์ด์ ๋ชจ๋ ๋ฌธ์๋ฅผ ๊ฒ์ฌํ์ผ๋ฉด |
| 97 | + if (index === word.length) { |
| 98 | + return node.isEnd; |
| 99 | + } |
| 100 | + |
| 101 | + const char = word[index]; |
| 102 | + |
| 103 | + // ์์ผ๋์นด๋('.')์ธ ๊ฒฝ์ฐ |
| 104 | + if (char === '.') { |
| 105 | + // ํ์ฌ ๋
ธ๋์ ๋ชจ๋ ์์์ ๋ํด ์ฌ๊ท์ ์ผ๋ก ๊ฒ์ |
| 106 | + for (const key in node.children) { |
| 107 | + if (this.searchWithWildcard(word, index + 1, node.children[key])) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | + // ์ผ๋ฐ ๋ฌธ์์ธ ๊ฒฝ์ฐ |
| 114 | + else { |
| 115 | + // ํด๋น ๋ฌธ์์ ๋ํ ์์ ๋
ธ๋๊ฐ ์์ผ๋ฉด false |
| 116 | + if (!node.children[char]) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + // ๋ค์ ๋ฌธ์ ๊ฒ์ |
| 121 | + return this.searchWithWildcard(word, index + 1, node.children[char]); |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +/** |
| 126 | + * Your WordDictionary object will be instantiated and called as such: |
| 127 | + * var obj = new WordDictionary() |
| 128 | + * obj.addWord(word) |
| 129 | + * var param_2 = obj.search(word) |
| 130 | + */ |
0 commit comments