-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/lledellebell/leetcode-study
- Loading branch information
Showing
2 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* @problem | ||
* Trie๋ฅผ ๊ตฌํํฉ๋๋ค. | ||
* - ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ต์ ํํ๋ฉด์ ์ ํํ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฅํด์ผ ํฉ๋๋ค. | ||
* | ||
* @example | ||
* const trie = new Trie(); | ||
* trie.insert("apple"); // undefined | ||
* trie.search("apple"); // true | ||
* trie.search("app"); // false | ||
* trie.startsWith("app"); // true | ||
* trie.insert("app"); // undefined | ||
* trie.search("app"); // true | ||
* | ||
* @description | ||
* - ์๊ฐ๋ณต์ก๋: | ||
* ใด insert: O(m) (m์ ๋จ์ด ๊ธธ์ด) | ||
* ใด search: O(m) (m์ ๋จ์ด ๊ธธ์ด) | ||
* ใด startsWith: O(m) (m์ ์ ๋์ฌ ๊ธธ์ด) | ||
* - ๊ณต๊ฐ๋ณต์ก๋: O(ALPHABET_SIZE * m * n) | ||
* ใด ALPHABET_SIZE: ๋ฌธ์์ด์ ์ํ๋ฒณ ์ (์๋ฌธ์ ๊ฒฝ์ฐ 26) | ||
* ใด m: ๋จ์ด์ ํ๊ท ๊ธธ์ด | ||
*/ | ||
class TrieNode { | ||
constructor() { | ||
// ๊ฐ ๋ฌธ์๋ฅผ ํค๋ก ํ๊ณ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ผ๋ก ํ๋ ๊ฐ์ฒด | ||
this.children = {}; | ||
// ํ์ฌ ๋ ธ๋๊ฐ ๋จ์ด์ ๋ง์ง๋ง ๋ฌธ์์ธ์ง ํ์ํ๋ ํ๋๊ทธ | ||
this.isEndOfWord = false; | ||
} | ||
} | ||
|
||
class Trie { | ||
constructor() { | ||
// ๋น ๋ฌธ์์ด์ ๋ํ๋ด๋ ๋ฃจํธ ๋ ธ๋ ์์ฑ | ||
this.root = new TrieNode(); | ||
} | ||
|
||
/** | ||
* ๋จ์ด๋ฅผ Trie์ ์ฝ์ ํ๋ ๋ฉ์๋ | ||
* @param {string} word - ์ฝ์ ํ ๋จ์ด | ||
*/ | ||
insert(word) { | ||
// ๋ฃจํธ ๋ ธ๋๋ถํฐ ์์ | ||
let node = this.root; | ||
|
||
// ๋จ์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ํ | ||
for (let i = 0; i < word.length; i++) { | ||
const char = word[i]; | ||
// ํ์ฌ ๋ฌธ์์ ๋ํ ๋ ธ๋๊ฐ ์์ผ๋ฉด ์๋ก ์์ฑ | ||
if (!(char in node.children)) { | ||
node.children[char] = new TrieNode(); | ||
} | ||
// ๋ค์ ๋ฌธ์๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์์ ๋ ธ๋๋ก ์ด๋ | ||
node = node.children[char]; | ||
} | ||
// ๋จ์ด์ ๋ง์ง๋ง ๋ฌธ์์์ ํ์ | ||
node.isEndOfWord = true; | ||
} | ||
|
||
|
||
/** | ||
* ์ ํํ ๋จ์ด๊ฐ ์กด์ฌํ๋์ง ๊ฒ์ํ๋ ๋ฉ์๋ | ||
* @param {string} word - ๊ฒ์ํ ๋จ์ด | ||
* @returns {boolean} - ๋จ์ด ์กด์ฌ ์ฌ๋ถ | ||
*/ | ||
search(word) { | ||
// ๋จ์ด๋ฅผ ์ฐพ์ ๋ง์ง๋ง ๋ ธ๋๋ฅผ ๋ฐํ | ||
const node = this._traverse(word); | ||
// ๋จ์ด๊ฐ ์กด์ฌํ๊ณ ํด๋น ๋ ธ๋๊ฐ ๋จ์ด์ ๋์ธ ๊ฒฝ์ฐ์๋ง true ๋ฐํ | ||
return node !== null && node.isEndOfWord; | ||
} | ||
|
||
/** | ||
* ์ฃผ์ด์ง ์ ๋์ฌ๋ก ์์ํ๋ ๋จ์ด๊ฐ ์กด์ฌํ๋์ง ๊ฒ์ํ๋ ๋ฉ์๋ | ||
* @param {string} prefix - ๊ฒ์ํ ์ ๋์ฌ | ||
* @returns {boolean} - ์ ๋์ฌ๋ก ์์ํ๋ ๋จ์ด ์กด์ฌ ์ฌ๋ถ | ||
*/ | ||
startsWith(prefix) { | ||
// ์ ๋์ฌ์ ํด๋นํ๋ ๋ ธ๋๊ฐ ์กด์ฌํ๋์ง๋ง ํ์ธ | ||
return this._traverse(prefix) !== null; | ||
} | ||
|
||
/** | ||
* ๋ฌธ์์ด์ ๋ฐ๋ผ๊ฐ๋ฉฐ ๋ง์ง๋ง ๋ ธ๋๋ฅผ ๋ฐํํ๋ ๋ด๋ถ ํฌํผ ๋ฉ์๋ | ||
* @param {string} str - ํ์ํ ๋ฌธ์์ด | ||
* @returns {TrieNode|null} - ๋ง์ง๋ง ๋ฌธ์์ ํด๋นํ๋ ๋ ธ๋ ๋๋ null | ||
* @private | ||
*/ | ||
_traverse(str) { | ||
// ๋ฃจํธ ๋ ธ๋๋ถํฐ ์์ | ||
let node = this.root; | ||
|
||
// ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ํ | ||
for (let i = 0; i < str.length; i++) { | ||
const char = str[i]; | ||
// ํ์ฌ ๋ฌธ์์ ๋ํ ๋ ธ๋๊ฐ ์์ผ๋ฉด null ๋ฐํ | ||
if (!(char in node.children)) { | ||
return null; | ||
} | ||
// ๋ค์ ๋ฌธ์๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์์ ๋ ธ๋๋ก ์ด๋ | ||
node = node.children[char]; | ||
} | ||
// ๋ง์ง๋ง ๋ ธ๋ ๋ฐํ | ||
return node; | ||
} | ||
} | ||
|
||
const trie = new Trie(); | ||
console.log(trie.insert("apple")); // undefined | ||
console.log(trie.search("apple")); // true | ||
console.log(trie.search("app")); // false | ||
console.log(trie.startsWith("app")); // true | ||
console.log(trie.insert("app")); // undefined | ||
console.log(trie.search("app")); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @problem | ||
* ์ฃผ์ด์ง ๋ฌธ์์ด s๊ฐ ๋จ์ด ์ฌ์ wordDict์ ํฌํจ๋ ๋จ์ด๋ค๋ก๋ง ๊ตฌ์ฑ๋ ์ ์๋์ง ํ์ธํ๋ ๋ฌธ์ ์ ๋๋ค. | ||
* ๋จ์ด ์ฌ์ ์ ๋จ์ด๋ค์ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ฌธ์์ด s๋ฅผ ์์ ํ ๋๋ ์ ์์ด์ผ ํฉ๋๋ค. | ||
* | ||
* @constraints | ||
* - 1 <= s.length <= 300 | ||
* - 1 <= wordDict.length <= 1000 | ||
* - 1 <= wordDict[i].length <= 20 | ||
* - s์ wordDict[i]๋ ๋ชจ๋ ์๋ฌธ์ ์ํ๋ฒณ์ผ๋ก๋ง ๊ตฌ์ฑ๋ฉ๋๋ค. | ||
* | ||
* @param {string} s - ์ฃผ์ด์ง ๋ฌธ์์ด | ||
* @param {string[]} wordDict - ๋จ์ด ์ฌ์ | ||
* @returns {boolean} ๋ฌธ์์ด s๊ฐ ๋จ์ด ์ฌ์ ์ ๋จ์ด๋ค๋ก๋ง ๋๋ ์ ์๋์ง ์ฌ๋ถ | ||
* | ||
* @example | ||
* - ์์ 1: | ||
* ใด Input: s = "leetcode", wordDict = ["leet", "code"] | ||
* ใด Output: true | ||
* ใด Explanation: "leetcode"๋ "leet" + "code"๋ก ๋๋ ์ ์์ต๋๋ค. | ||
* - ์์ 2: | ||
* ใด Input: s = "applepenapple", wordDict = ["apple", "pen"] | ||
* ใด Output: true | ||
* ใด Explanation: "applepenapple"๋ "apple" + "pen" + "apple"๋ก ๋๋ ์ ์์ต๋๋ค. | ||
* - ์์ 3: | ||
* ใด Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] | ||
* ใด Output: false | ||
* ใด Explanation: "catsandog"๋ wordDict์ ๋จ์ด๋ค๋ก ๋๋ ์ ์์ต๋๋ค. | ||
* | ||
* @description | ||
* - ์๊ฐ ๋ณต์ก๋: O(n^2) | ||
* ใด ์ธ๋ถ ๋ฐ๋ณต๋ฌธ: ๋ฌธ์์ด s์ ๊ธธ์ด n์ ๋ํด 1๋ถํฐ n๊น์ง ๋ฐ๋ณต (O(n)) | ||
* ใด ๋ด๋ถ ๋ฐ๋ณต๋ฌธ: ๊ฐ i์ ๋ํด ์ต๋ i๋ฒ ๋ฐ๋ณต (O(n)) | ||
* ใด substring ๋ฐ Set ๊ฒ์: O(1) (substring์ ๋ด๋ถ์ ์ผ๋ก O(k)์ด์ง๋ง, k๋ ์ต๋ ๋จ์ด ๊ธธ์ด๋ก ๊ฐ์ฃผ) | ||
* ใด ๊ฒฐ๊ณผ์ ์ผ๋ก O(n^2)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(n + m) | ||
* ใด dp ๋ฐฐ์ด์ ํฌ๊ธฐ: s์ ๊ธธ์ด n + 1 (O(n)) | ||
* ใด wordSet: wordDict์ ๋จ์ด ๊ฐ์์ ๋น๋ก (O(m), m์ wordDict์ ๋จ์ด ์) | ||
*/ | ||
function wordBreak(s, wordDict) { | ||
// wordDict๋ฅผ Set์ผ๋ก ๋ณํํ์ฌ ๊ฒ์ ์๋๋ฅผ O(1)๋ก ๋ง๋ฆ | ||
const wordSet = new Set(wordDict); | ||
|
||
// dp ๋ฐฐ์ด ์์ฑ: dp[i]๋ s์ ์ฒ์๋ถํฐ i๋ฒ์งธ ๋ฌธ์๊น์ง๊ฐ wordDict์ ๋จ์ด๋ค๋ก ๋๋ ์ ์๋์ง๋ฅผ ๋ํ๋ | ||
const dp = new Array(s.length + 1).fill(false); | ||
dp[0] = true; // ๋น ๋ฌธ์์ด์ ํญ์ ๋๋ ์ ์์ | ||
|
||
// i๋ ๋ฌธ์์ด์ ๋ ์ธ๋ฑ์ค๋ฅผ ๋ํ๋ | ||
for (let i = 1; i <= s.length; i++) { | ||
// j๋ ๋ฌธ์์ด์ ์์ ์ธ๋ฑ์ค๋ฅผ ๋ํ๋ | ||
for (let j = 0; j < i; j++) { | ||
// dp[j]๊ฐ true์ด๊ณ , s[j:i]๊ฐ wordSet์ ํฌํจ๋์ด ์๋ค๋ฉด | ||
if (dp[j] && wordSet.has(s.substring(j, i))) { | ||
dp[i] = true; // dp[i]๋ฅผ true๋ก ์ค์ | ||
break; // ๋ ์ด์ ํ์ธํ ํ์ ์์ | ||
} | ||
} | ||
} | ||
|
||
// dp[s.length]๊ฐ true๋ผ๋ฉด ๋ฌธ์์ด s๋ฅผ wordDict์ ๋จ์ด๋ค๋ก ๋๋ ์ ์์ | ||
return dp[s.length]; | ||
} | ||
|
||
const s1 = "leetcode"; | ||
const wordDict1 = ["leet", "code"]; | ||
console.log(wordBreak(s1, wordDict1)); // true | ||
|
||
const s2 = "applepenapple"; | ||
const wordDict2 = ["apple", "pen"]; | ||
console.log(wordBreak(s2, wordDict2)); // true | ||
|
||
const s3 = "catsandog"; | ||
const wordDict3 = ["cats", "dog", "sand", "and", "cat"]; | ||
console.log(wordBreak(s3, wordDict3)); // false |