diff --git a/longest-palindromic-substring/gitsunmin.ts b/longest-palindromic-substring/gitsunmin.ts new file mode 100644 index 000000000..535c01c1a --- /dev/null +++ b/longest-palindromic-substring/gitsunmin.ts @@ -0,0 +1,45 @@ +/** + * https://leetcode.com/problems/longest-palindromic-substring/ + * time complexity : O(n) + * space complexity : O(n^2) + */ + +function findPalindromeAroundCenter(s: string, leftIndex: number, rightIndex: number): [number, number] { + while (leftIndex >= 0 && rightIndex < s.length && s[leftIndex] === s[rightIndex]) { + leftIndex--; // 왼쪽으로 확장 + rightIndex++; // 오른쪽으로 확장 + } + + // 팰린드롬의 시작과 끝 인덱스 반환 + return [leftIndex + 1, rightIndex - 1]; +} + +function longestPalindrome(s: string): string { + if (s.length <= 1) return s; + + let longestPalindromeStartIndex = 0; + let longestPalindromeLength = 0; + + for (let centerIndex = 0; centerIndex < s.length; centerIndex++) { + // 홀수 길이 팰린드롬 확인 + const [oddStart, oddEnd] = findPalindromeAroundCenter(s, centerIndex, centerIndex); + const oddLength = oddEnd - oddStart + 1; + + if (oddLength > longestPalindromeLength) { + longestPalindromeStartIndex = oddStart; + longestPalindromeLength = oddLength; + } + + // 짝수 길이 팰린드롬 확인 + const [evenStart, evenEnd] = findPalindromeAroundCenter(s, centerIndex, centerIndex + 1); + const evenLength = evenEnd - evenStart + 1; + + if (evenLength > longestPalindromeLength) { + longestPalindromeStartIndex = evenStart; + longestPalindromeLength = evenLength; + } + } + + // 가장 긴 팰린드롬 부분 문자열 반환 + return s.substring(longestPalindromeStartIndex, longestPalindromeStartIndex + longestPalindromeLength); +} diff --git a/subtree-of-another-tree/gitsunmin.ts b/subtree-of-another-tree/gitsunmin.ts new file mode 100644 index 000000000..21e156a2c --- /dev/null +++ b/subtree-of-another-tree/gitsunmin.ts @@ -0,0 +1,29 @@ +/** + * https://leetcode.com/problems/subtree-of-another-tree/ + * time complexity : O(n * m) + * space complexity : O(n) + */ + +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val ?? 0; + this.left = left ?? null; + this.right = right ?? null; + } +} + +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + if (!p && !q) return true; + if (!p || !q || p.val !== q.val) return false; + + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); +} + +function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean { + if (!root) return false; + if (isSameTree(root, subRoot)) return true; + return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); +} diff --git a/validate-binary-search-tree/gitsunmin.ts b/validate-binary-search-tree/gitsunmin.ts new file mode 100644 index 000000000..9d0c2c9e2 --- /dev/null +++ b/validate-binary-search-tree/gitsunmin.ts @@ -0,0 +1,32 @@ +/** + * https://leetcode.com/problems/validate-binary-search-tree/ + * time complexity : O(n) + * space complexity : O(n) + */ + +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val ?? 0; + this.left = left ?? null; + this.right = right ?? null; + } +} + +function validate(node: TreeNode | null, lower: number, upper: number): boolean { + if (!node) return true; + const val = node.val; + + if (val <= lower || val >= upper) return false; + if (!validate(node.right, val, upper)) return false; + if (!validate(node.left, lower, val)) return false; + + return true; +} + +function isValidBST(root: TreeNode | null): boolean { + + return validate(root, -Infinity, Infinity); +}