Skip to content

[HoonDongKang] Week 15 Solutions #1662

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 5 commits into from
Jul 13, 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
75 changes: 75 additions & 0 deletions alien-dictionary/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* [Problem]: [892] Alien Dictionary
* (https://www.lintcode.com/problem/892/)
*/

//시간복잡도 O(n+e)
//공간복잡도 O(n+e)
export class Solution {
/**
* @param words: a list of words
* @return: a string which is correct order
*/
alienOrder(words: string[]): string {
// Write your code here
const graph: Map<string, Set<string>> = new Map();

// 모든 문자를 그래프 노드로 초기화
for (const word of words) {
for (const char of word) {
if (!graph.has(char)) {
graph.set(char, new Set());
}
}
}

// 문자 간 선후관계(edge) 설정
for (let i = 1; i < words.length; i++) {
const prev = words[i - 1];
const curr = words[i];
let isDiff = false;

for (let j = 0; j < Math.min(prev.length, curr.length); j++) {
const p = prev[j];
const c = curr[j];

if (p !== c) {
graph.get(p)!.add(c);
isDiff = true;
break;
}
}

if (!isDiff && prev.length > curr.length) {
return "";
}
}

const visited: Set<string> = new Set();
const visiting: Set<string> = new Set();
const output: string[] = [];

function dfs(node: string): boolean {
if (visited.has(node)) return true;
if (visiting.has(node)) return false;

visiting.add(node);

for (const neighbor of graph.get(node)!) {
if (!dfs(neighbor)) return false;
}

visiting.delete(node);
visited.add(node);
output.push(node);

return true;
}

for (const node of graph.keys()) {
if (!dfs(node)) return "";
}

return output.reverse().join("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* [Problem]: [105] Construct Binary Tree From Preorder And Inorder Traversal
* (https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/)
*/

class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}

//시간복잡도 O(n)
//공간복잡도 O(n)
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
if (!preorder.length || !inorder.length) return null;

const map = new Map<number, number>();
let idx = 0;

inorder.forEach((v, i) => map.set(v, i));

function dfs(left: number, right: number): TreeNode | null {
if (left > right) return null;

const value = preorder[idx++];
const root = new TreeNode(value);
const index = map.get(value)!;

root.left = dfs(left, index - 1);
root.right = dfs(index + 1, right);

return root;
}

return dfs(0, inorder.length - 1);
}
31 changes: 31 additions & 0 deletions longest-palindromic-substring/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* [Problem]: [5] Longest Palindromic Substring
* (https://leetcode.com/problems/longest-palindromic-substring/)
*/
// 시간복잡도 O(n^2)
// 공간복잡도 O(1)
function longestPalindrome(s: string): string {
if (s.length < 2) return s;

let start = 0;
let end = 0;

function findPalindrome(left: number, right: number): void {
while (0 <= left && right < s.length && s[left] === s[right]) {
left--;
right++;
}

if (right - left - 1 > end - start) {
start = left + 1;
end = right - 1;
}
}

for (let i = 0; i < s.length; i++) {
findPalindrome(i, i);
findPalindrome(i, i + 1);
}

return s.substring(start, end + 1);
}
22 changes: 22 additions & 0 deletions rotate-image/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* [Problem]: [48] Rotate Image
* (https://leetcode.com/problems/rotate-image/)
*/
/**
Do not return anything, modify matrix in-place instead.
*/
// 시간복잡도 O(n^2)
// 공간복잡도 O(1)
function rotate(matrix: number[][]): void {
const length = matrix.length;

for (let i = 0; i < length; i++) {
for (let j = i + 1; j < length; j++) {
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
}
}

for (let i = 0; i < length; i++) {
matrix[i].reverse();
}
}
26 changes: 26 additions & 0 deletions subtree-of-another-tree/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* [Problem]: [572] Subtree of Another Tree
* (https://leetcode.com/problems/subtree-of-another-tree/description/)
*/

class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}

//시간복잡도 O(n*m)
//공간복잡도 O(n+m)
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
const pre = (node: TreeNode | null) => {
if (!node) return "N";
return `(${node.val},${pre(node.left)},${pre(node.right)})`;
};

return pre(root).includes(pre(subRoot));
}