Skip to content

[printjin-gmailcom] WEEK 15 solutions #1653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions alien-dictionary/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -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 ""
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions longest-palindromic-substring/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions rotate-image/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 13 additions & 0 deletions subtree-of-another-tree/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -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)