-
-
Notifications
You must be signed in to change notification settings - Fork 195
[soobing] Week 05 Solutions #1379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,34 @@ | ||
class Solution { | ||
/** | ||
* 문자열 배열을 하나의 문자열로 인코딩합니다. | ||
* @param strs - 문자열 배열 | ||
* @returns 인코딩된 하나의 문자열 | ||
*/ | ||
encode(strs: string[]): string { | ||
return strs.map((str) => str.length + "#" + str).join(""); | ||
} | ||
|
||
/** | ||
* 인코딩된 문자열을 원래 문자열 배열로 디코딩합니다. | ||
* @param str - 인코딩된 문자열 | ||
* @returns 디코딩된 문자열 배열 | ||
*/ | ||
decode(str: string): string[] { | ||
const result: string[] = []; | ||
|
||
let i = 0; | ||
while (i < str.length) { | ||
let j = i; | ||
while (str[j] !== "#") { | ||
j++; | ||
} | ||
|
||
const length = parseInt(str.slice(i, j)); | ||
const word = str.slice(j + 1, j + 1 + length); | ||
result.push(word); | ||
i = j + 1 + length; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains hidden or 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,15 @@ | ||
// idea: 배열에 담긴 모든 애들을 다 sorting하면서 sorting된 결과를 key로 바인딩하고 Record<string, string[]> 에 맞게 매핑하여 values들만 리턴하면 될것 같음 | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const map = new Map<string, string[]>(); | ||
|
||
for (let i = 0; i < strs.length; i++) { | ||
const key = strs[i].split("").sort().join(""); | ||
const group = map.get(key); | ||
if (group) { | ||
group.push(strs[i]); | ||
} else { | ||
map.set(key, [strs[i]]); | ||
} | ||
} | ||
return [...map.values()]; | ||
} |
This file contains hidden or 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,46 @@ | ||
class TrieNode { | ||
children: Map<string, TrieNode>; | ||
isEnd: boolean; | ||
|
||
constructor() { | ||
this.children = new Map(); | ||
this.isEnd = false; | ||
} | ||
} | ||
|
||
class Trie { | ||
root: TrieNode; | ||
|
||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
|
||
insert(word: string): void { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node.children.has(char)) { | ||
node.children.set(char, new TrieNode()); | ||
} | ||
node = node.children.get(char)!; | ||
} | ||
node.isEnd = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
const node = this._findNode(word); | ||
return node !== null && node.isEnd; | ||
} | ||
|
||
startsWith(prefix: string): boolean { | ||
return this._findNode(prefix) !== null; | ||
} | ||
|
||
private _findNode(word: string): TrieNode | null { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node.children.has(char)) return null; | ||
node = node.children.get(char)!; | ||
} | ||
return node; | ||
} | ||
} |
This file contains hidden or 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,15 @@ | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
const dp = new Array(s.length + 1).fill(false); | ||
dp[s.length] = true; | ||
|
||
for (let i = s.length - 1; i >= 0; i--) { | ||
for (const word of wordDict) { | ||
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) { | ||
dp[i] = dp[i + word.length]; | ||
} | ||
|
||
if (dp[i]) break; | ||
} | ||
} | ||
return dp[0]; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.