From 47081ea2aadb473b6d9f54ff5fabd539c87fd7ed Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Mon, 27 Oct 2025 11:34:04 +0900 Subject: [PATCH 1/5] feat: Solve subtree-of-another-tree problem --- subtree-of-another-tree/hu6r1s.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 subtree-of-another-tree/hu6r1s.py diff --git a/subtree-of-another-tree/hu6r1s.py b/subtree-of-another-tree/hu6r1s.py new file mode 100644 index 000000000..2804e8207 --- /dev/null +++ b/subtree-of-another-tree/hu6r1s.py @@ -0,0 +1,24 @@ +# 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 subRoot: + return True + if not root: + return False + + def same(root, subRoot): + if not root or not subRoot: + return not root and not subRoot + + if root.val != subRoot.val: + return False + return same(root.left, subRoot.left) and same(root.right, subRoot.right) + + if same(root, subRoot): + return True + return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot) From 8c46d003f278ae3e9ff0bbf1f4ba2ba812ad7cf3 Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Fri, 31 Oct 2025 11:04:06 +0900 Subject: [PATCH 2/5] feat: Solve construct-binary-tree-from-preorder-and-inorder-traversal problem --- .../hu6r1s.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/hu6r1s.py diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/hu6r1s.py b/construct-binary-tree-from-preorder-and-inorder-traversal/hu6r1s.py new file mode 100644 index 000000000..5b5c8befd --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/hu6r1s.py @@ -0,0 +1,16 @@ +# 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 inorder: + return None + + val = preorder.pop(0) + mid = inorder.index(val) + left = self.buildTree(preorder, inorder[:mid]) + right = self.buildTree(preorder, inorder[mid + 1 :]) + return TreeNode(val, left, right) From 0fbc61149a9d096eb3c21a8a907ba41b8b13f4e3 Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Fri, 31 Oct 2025 11:25:34 +0900 Subject: [PATCH 3/5] feat: Solve longest-palindromic-substring problem --- longest-palindromic-substring/hu6r1s.py | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 longest-palindromic-substring/hu6r1s.py diff --git a/longest-palindromic-substring/hu6r1s.py b/longest-palindromic-substring/hu6r1s.py new file mode 100644 index 000000000..f528f2ead --- /dev/null +++ b/longest-palindromic-substring/hu6r1s.py @@ -0,0 +1,26 @@ +class Solution: + def longestPalindrome(self, s: str) -> str: + # if len(s) <= 2: + # return s[0] + + # for i in range(2, len(s)): + # for k in range(len(s)-i): + # if s[k:k+i] == s[k:k+i][::-1]: + # return s[k:k+i] + + max_s, max_e = 0, 0 + + for i in range(len(s)): + start, end = i, i + while 0 <= start and end < len(s) and s[start] == s[end]: + if max_e - max_s < end - start: + max_s, max_e = start, end + start, end = start - 1, end + 1 + + start, end = i, i + 1 + while 0 <= start and end < len(s) and s[start] == s[end]: + if max_e - max_s < end - start: + max_s, max_e = start, end + start, end = start - 1, end + 1 + + return s[max_s : max_e + 1] From 48cbc5436637afb35e72afb1f0b192f2ff8a61c2 Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Sat, 1 Nov 2025 12:51:53 +0900 Subject: [PATCH 4/5] feat: Solve rotate-image problem --- rotate-image/hu6r1s.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 rotate-image/hu6r1s.py diff --git a/rotate-image/hu6r1s.py b/rotate-image/hu6r1s.py new file mode 100644 index 000000000..27fe37f4d --- /dev/null +++ b/rotate-image/hu6r1s.py @@ -0,0 +1,20 @@ +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + [0][0] [0][1] [0][2] + [1][0] [1][1] [1][2] + [2][0] [2][1] [2][2] + + [2][0] [1][0] [0][0] + [2][1] [1][1] [0][1] + [2][2] [1][2] [0][2] + """ + n = len(matrix) + + for i in range(n): + for j in range(i, n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + + for i in range(n): + matrix[i].reverse() From 01bbd93f456a46576840efd76eee82b866637657 Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Sat, 1 Nov 2025 13:03:12 +0900 Subject: [PATCH 5/5] feat: Solve alien-dictionary problem --- alien-dictionary/hu6r1s.py | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 alien-dictionary/hu6r1s.py diff --git a/alien-dictionary/hu6r1s.py b/alien-dictionary/hu6r1s.py new file mode 100644 index 000000000..e605f22d3 --- /dev/null +++ b/alien-dictionary/hu6r1s.py @@ -0,0 +1,44 @@ +from typing import ( + List, +) + +import heapq + heap = [c for c in indegree if indegree[c] == 0] + heapq.heapify(heap) + res = [] + + +class Solution: + """ + @param words: a list of words + @return: a string which is correct order + """ + def alien_order(self, words: List[str]) -> str: + # Write your code here + adj = {c: set() for word in words for c in word} + indegree = {c: 0 for c in adj} + + for i in range(len(words) - 1): + w1, w2 = words[i], words[i+1] + minlen = min(len(w1), len(w2)) + if len(w1) > len(w2) and w1[:minlen] == w2[:minlen]: + return "" + for j in range(minlen): + if w1[j] != w2[j]: + if w2[j] not in adj[w1[j]]: + adj[w1[j]].add(w2[j]) + indegree[w2[j]] += 1 + break + + while heap: + c = heapq.heappop(heap) + res.append(c) + for nei in adj[c]: + indegree[nei] -= 1 + if indegree[nei] == 0: + heapq.heappush(heap, nei) + + if len(res) != len(adj): + return "" + + return "".join(res)