Skip to content

[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 4 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions encode-and-decode-strings/soobing.ts
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;
}
}
15 changes: 15 additions & 0 deletions group-anagrams/soobing.ts
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()];
}
46 changes: 46 additions & 0 deletions implement-trie-prefix-tree/soobing.ts
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;
}
}
15 changes: 15 additions & 0 deletions word-break/soobing.ts
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];
}