diff --git a/alien-dictionary/printjin-gmailcom.py b/alien-dictionary/printjin-gmailcom.py new file mode 100644 index 000000000..de66ac1d6 --- /dev/null +++ b/alien-dictionary/printjin-gmailcom.py @@ -0,0 +1,29 @@ +import collections +class Solution: + def alien_order(self, words): + graph = collections.defaultdict(set) + indegree = {} + for word in words: + for c in word: + indegree[c] = 0 + for i in range(len(words) - 1): + w1, w2 = words[i], words[i + 1] + min_len = min(len(w1), len(w2)) + if len(w1) > len(w2) and w1[:min_len] == w2[:min_len]: + return "" + for j in range(min_len): + if w1[j] != w2[j]: + if w2[j] not in graph[w1[j]]: + graph[w1[j]].add(w2[j]) + indegree[w2[j]] += 1 + break + queue = collections.deque([c for c in indegree if indegree[c] == 0]) + res = [] + while queue: + c = queue.popleft() + res.append(c) + for nei in graph[c]: + indegree[nei] -= 1 + if indegree[nei] == 0: + queue.append(nei) + return "".join(res) if len(res) == len(indegree) else "" diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/printjin-gmailcom.py b/construct-binary-tree-from-preorder-and-inorder-traversal/printjin-gmailcom.py new file mode 100644 index 000000000..278870291 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/printjin-gmailcom.py @@ -0,0 +1,14 @@ +class Solution: + def buildTree(self, preorder, inorder): + inorder_index_map = {val: idx for idx, val in enumerate(inorder)} + def build(pre_left, pre_right, in_left, in_right): + if pre_left > pre_right: + return None + root_val = preorder[pre_left] + root = TreeNode(root_val) + in_root_idx = inorder_index_map[root_val] + left_size = in_root_idx - in_left + root.left = build(pre_left + 1, pre_left + left_size, in_left, in_root_idx - 1) + root.right = build(pre_left + left_size + 1, pre_right, in_root_idx + 1, in_right) + return root + return build(0, len(preorder) - 1, 0, len(inorder) - 1) diff --git a/longest-palindromic-substring/printjin-gmailcom.py b/longest-palindromic-substring/printjin-gmailcom.py new file mode 100644 index 000000000..627f00527 --- /dev/null +++ b/longest-palindromic-substring/printjin-gmailcom.py @@ -0,0 +1,13 @@ +class Solution: + def longestPalindrome(self, s): + def expand(left, right): + 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)): + odd = expand(i, i) + even = expand(i, i + 1) + res = max(res, odd, even, key=len) + return res diff --git a/rotate-image/printjin-gmailcom.py b/rotate-image/printjin-gmailcom.py new file mode 100644 index 000000000..30809951d --- /dev/null +++ b/rotate-image/printjin-gmailcom.py @@ -0,0 +1,8 @@ +class Solution: + def rotate(self, matrix): + n = len(matrix) + for i in range(n): + for j in range(i + 1, n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + for row in matrix: + row.reverse() diff --git a/subtree-of-another-tree/printjin-gmailcom.py b/subtree-of-another-tree/printjin-gmailcom.py new file mode 100644 index 000000000..9ceefefc5 --- /dev/null +++ b/subtree-of-another-tree/printjin-gmailcom.py @@ -0,0 +1,13 @@ +class Solution: + def isSubtree(self, root, subRoot): + if not root: + return False + if self.isSameTree(root, subRoot): + return True + return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot) + def isSameTree(self, s, t): + if not s and not t: + return True + if not s or not t or s.val != t.val: + return False + return self.isSameTree(s.left, t.left) and self.isSameTree(s.right, t.right)