Skip to content

Commit e0ca496

Browse files
authored
Merge pull request #1379 from soobing/week5
[soobing] Week 05 Solutions
2 parents 919500c + 980e610 commit e0ca496

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

encode-and-decode-strings/soobing.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
/**
3+
* 문자열 배열을 하나의 문자열로 인코딩합니다.
4+
* @param strs - 문자열 배열
5+
* @returns 인코딩된 하나의 문자열
6+
*/
7+
encode(strs: string[]): string {
8+
return strs.map((str) => str.length + "#" + str).join("");
9+
}
10+
11+
/**
12+
* 인코딩된 문자열을 원래 문자열 배열로 디코딩합니다.
13+
* @param str - 인코딩된 문자열
14+
* @returns 디코딩된 문자열 배열
15+
*/
16+
decode(str: string): string[] {
17+
const result: string[] = [];
18+
19+
let i = 0;
20+
while (i < str.length) {
21+
let j = i;
22+
while (str[j] !== "#") {
23+
j++;
24+
}
25+
26+
const length = parseInt(str.slice(i, j));
27+
const word = str.slice(j + 1, j + 1 + length);
28+
result.push(word);
29+
i = j + 1 + length;
30+
}
31+
32+
return result;
33+
}
34+
}

group-anagrams/soobing.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// idea: 배열에 담긴 모든 애들을 다 sorting하면서 sorting된 결과를 key로 바인딩하고 Record<string, string[]> 에 맞게 매핑하여 values들만 리턴하면 될것 같음
2+
function groupAnagrams(strs: string[]): string[][] {
3+
const map = new Map<string, string[]>();
4+
5+
for (let i = 0; i < strs.length; i++) {
6+
const key = strs[i].split("").sort().join("");
7+
const group = map.get(key);
8+
if (group) {
9+
group.push(strs[i]);
10+
} else {
11+
map.set(key, [strs[i]]);
12+
}
13+
}
14+
return [...map.values()];
15+
}

implement-trie-prefix-tree/soobing.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class TrieNode {
2+
children: Map<string, TrieNode>;
3+
isEnd: boolean;
4+
5+
constructor() {
6+
this.children = new Map();
7+
this.isEnd = false;
8+
}
9+
}
10+
11+
class Trie {
12+
root: TrieNode;
13+
14+
constructor() {
15+
this.root = new TrieNode();
16+
}
17+
18+
insert(word: string): void {
19+
let node = this.root;
20+
for (const char of word) {
21+
if (!node.children.has(char)) {
22+
node.children.set(char, new TrieNode());
23+
}
24+
node = node.children.get(char)!;
25+
}
26+
node.isEnd = true;
27+
}
28+
29+
search(word: string): boolean {
30+
const node = this._findNode(word);
31+
return node !== null && node.isEnd;
32+
}
33+
34+
startsWith(prefix: string): boolean {
35+
return this._findNode(prefix) !== null;
36+
}
37+
38+
private _findNode(word: string): TrieNode | null {
39+
let node = this.root;
40+
for (const char of word) {
41+
if (!node.children.has(char)) return null;
42+
node = node.children.get(char)!;
43+
}
44+
return node;
45+
}
46+
}

word-break/soobing.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function wordBreak(s: string, wordDict: string[]): boolean {
2+
const dp = new Array(s.length + 1).fill(false);
3+
dp[s.length] = true;
4+
5+
for (let i = s.length - 1; i >= 0; i--) {
6+
for (const word of wordDict) {
7+
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) {
8+
dp[i] = dp[i + word.length];
9+
}
10+
11+
if (dp[i]) break;
12+
}
13+
}
14+
return dp[0];
15+
}

0 commit comments

Comments
 (0)