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) 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) 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] 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() 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)