From 973841f73c3b8c8e47cb248885d62718363786b7 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Mon, 7 Jul 2025 14:56:46 +0900 Subject: [PATCH 1/4] longest-palindromic-substring solution (py) --- longest-palindromic-substring/hi-rachel.py | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 longest-palindromic-substring/hi-rachel.py diff --git a/longest-palindromic-substring/hi-rachel.py b/longest-palindromic-substring/hi-rachel.py new file mode 100644 index 000000000..1311bc335 --- /dev/null +++ b/longest-palindromic-substring/hi-rachel.py @@ -0,0 +1,38 @@ +""" +https://leetcode.com/problems/longest-palindromic-substring/ + +문자열 s가 주어졌을 때, 가장 긴 팰린드롬 부분 문자열을 찾아서 반환하는 함수를 작성해라. + +문제 풀이 + +1. 문자열 `s`의 모든 인덱스 `i`를 기준으로, +2. 두 종류의 중심에서 팰린드롬을 확장해 본다: + - 홀수 길이: `s[i]`를 중심으로 좌우 확장 (`i, i`) + - 짝수 길이: `s[i]`와 `s[i+1]`을 중심으로 좌우 확장 (`i, i+1`) +3. 각 중심에서 while문으로 `s[left] == s[right]`인 동안 확장 +4. 가장 긴 팰린드롬 문자열을 계속 업데이트 +5. 최종적으로 가장 긴 팰린드롬을 반환한다 + +TC: O(n^2) +SC: O(1) +""" + + +class Solution: + def longestPalindrome(self, s: str) -> str: + def expand(left: int, right: int) -> str: + while left >= 0 and right < len(s) and s[left] == s[right]: + left -= 1 + right += 1 + + return s[left + 1 : right] + + res = "" + for i in range(len(s)): + temp1 = expand(i, i) # 홀수 길이 팰린드롬 + temp2 = expand(i, i+1) # 짝수 길이 팰린드롬 + if len(temp1) > len(res): + res = temp1 + if len(temp2) > len(res): + res = temp2 + return res From 65c6b7705a69a261df097ca25dbd62bb8b906349 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 12 Jul 2025 16:05:20 +0900 Subject: [PATCH 2/4] subtree-of-another-tree solution (py) --- subtree-of-another-tree/hi-rachel.py | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 subtree-of-another-tree/hi-rachel.py diff --git a/subtree-of-another-tree/hi-rachel.py b/subtree-of-another-tree/hi-rachel.py new file mode 100644 index 000000000..7ab9c463e --- /dev/null +++ b/subtree-of-another-tree/hi-rachel.py @@ -0,0 +1,43 @@ +""" +https://leetcode.com/problems/subtree-of-another-tree/description/ + +두 개의 이진 트리 root와 subRoot의 루트(최상위 노드)가 주어졌을 때, +root의 하위 트리 중 subRoot와 동일한 구조와 값이 있는 경우 참을 반환하고 그렇지 않은 경우 거짓을 반환 + +TC: O(n * m), n: root의 노드 수, m: subRoot의 노드 수 +SC: O(n + m) +""" + +from typing import Optional + +# Definition for a binary tree node. +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: + if not root: + return False + if not subRoot: + return True + + def isSameTree(p, q): + # 둘 중 하나라도 None 이면, + if not p or not q: + # 둘 다 None 일 때만 True + return not p and not q + # 같이 다르면 False + if p.val != q.val: + return False + # 왼쪽과 오른쪽 서브트리를 재귀적으로 비교 + return isSameTree(p.left, q.left) and isSameTree(p.right, q.right) + + # root에서 시작해 subRoot와 동일한 구조의 트리인지 확인 + if isSameTree(root, subRoot): + return True + + # 왼쪽 또는 오른쪽 서브트리 중 하나라도 subRoot와 같은 트리가 있는지 재귀 탐색 + return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot) From 0cd8a25cb8982e504d3d9163d0e6d96cd4ea2b43 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 12 Jul 2025 16:46:38 +0900 Subject: [PATCH 3/4] construct-binary-tree-from-preorder-and-inorder-traversal solution (py) --- .../hi-rachel.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/hi-rachel.py diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/hi-rachel.py b/construct-binary-tree-from-preorder-and-inorder-traversal/hi-rachel.py new file mode 100644 index 000000000..5a883a7a8 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/hi-rachel.py @@ -0,0 +1,36 @@ +""" +https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ +두 개의 정수 배열 preorder와 inoder가 주어졌을 때 이진 트리를 구성하고 반환하시오. +preorder = 전위 순회 : 루트 -> 왼쪽 서브트리 -> 오른쪽 서브트리 +inorder = 중위 순회 : 왼쪽 서브트리 -> 루트 -> 오른쪽 서브트리 + +TC: O(n), n: 노드 수 +SC: O(n) +""" + +from typing import Optional, List + +# Definition for a binary tree node. +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if not preorder or not inorder: + return None + + # 1. preorder의 첫 값이 root + root_val = preorder[0] + root = TreeNode(root_val) + + # 2. inorder에서 root 위치 찾기 + root_index = inorder.index(root_val) + + # 3. 왼쪽, 오른쪽 나눠서 재귀적으로 연결 + root.left = self.buildTree(preorder[1:root_index + 1], inorder[:root_index]) + root.right = self.buildTree(preorder[root_index + 1:], inorder[root_index + 1:]) + + return root From 24a2c2d8ac50e14133da018d45ab76bf11b00ac0 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 12 Jul 2025 17:36:42 +0900 Subject: [PATCH 4/4] rotate-image solution (py) --- rotate-image/hi-rachel.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 rotate-image/hi-rachel.py diff --git a/rotate-image/hi-rachel.py b/rotate-image/hi-rachel.py new file mode 100644 index 000000000..f1bc0217f --- /dev/null +++ b/rotate-image/hi-rachel.py @@ -0,0 +1,28 @@ +""" +https://leetcode.com/problems/rotate-image/description/ + +TC: O(n^2) +SC: O(1) +""" + +from typing import List + +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + top, bottom = 0, len(matrix) - 1 + + while top < bottom: + left, right = top, bottom + + for i in range(bottom - top): + topLeft = matrix[top][left + i] + matrix[top][left + i] = matrix[bottom - i][left] + matrix[bottom - i][left] = matrix[bottom][right - i] + matrix[bottom][right - i] = matrix[top + i][right] + matrix[top + i][right] = topLeft + + top, bottom = top + 1, bottom - 1 +