diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/sun912.py b/construct-binary-tree-from-preorder-and-inorder-traversal/sun912.py new file mode 100644 index 000000000..08b08f65a --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/sun912.py @@ -0,0 +1,19 @@ +""" +TC: O(n) +SC: O(n) +""" +# 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 inorder: + idx = inorder.index(preorder.pop(0)) + node = TreeNode(inorder[idx]) + node.left = self.buildTree(preorder, inorder[0:idx]) + node.right = self.buildTree(preorder, inorder[idx+1:]) + + return node diff --git a/counting-bits/sun912.py b/counting-bits/sun912.py new file mode 100644 index 000000000..267490661 --- /dev/null +++ b/counting-bits/sun912.py @@ -0,0 +1,18 @@ +""" +TC: O(n) +SC: O(n) +""" + + +class Solution: + def countBits(self, n: int) -> List[int]: + dp = [0] * (n+1) + offset = 1 + + for i in range(1, n+1): + if offset * 2 == i: + offset = i + dp[i] = 1 + dp[i-offset] + return dp + + diff --git a/decode-ways/sun912.py b/decode-ways/sun912.py new file mode 100644 index 000000000..a99e06c33 --- /dev/null +++ b/decode-ways/sun912.py @@ -0,0 +1,24 @@ +""" + TC: O(n) + SC: O(n) + + DNF, read solutions by Dale...ㅠㅠ +""" + +class Solution: + def numDecodings(self, s: str) -> int: + memo = {len(s): 1} + + def dfs(start): + if start in memo: + return memo[start] + + if s[start] == "0": + return 0 + if start + 1 < len(s) and int(s[start: start+2]) < 27: + memo[start] = dfs(start + 1) + dfs(start + 2) + else: + memo[start] = dfs(start + 1) + return memo[start] + + return dfs(0) diff --git a/encode-and-decode-strings/sun912.py b/encode-and-decode-strings/sun912.py new file mode 100644 index 000000000..e2fa9f806 --- /dev/null +++ b/encode-and-decode-strings/sun912.py @@ -0,0 +1,30 @@ +""" +TC: O(n) + +""" +class Solution: + """ + @param: strs: a list of strings + @return: encodes a list of strings to a single string. + """ + def encode(self, strs): + result = "" + for str in strs: + result += str(len(str)) + "#" + str + return result + + """ + @param: str: A string + @return: decodes a single string to a list of strings + """ + def decode(self, str): + result = [] + idx = 0 + while idx < len(str): + temp_idx = idx + while str[temp_idx] != "#": + temp_idx += 1 + length = int(str[idx:temp_idx]) + result.append(str[temp_idx+1:temp_idx+length+1]) + idx = temp_idx + length + 1 + return result diff --git a/valid-anagram/sun912.py b/valid-anagram/sun912.py new file mode 100644 index 000000000..7b1ced037 --- /dev/null +++ b/valid-anagram/sun912.py @@ -0,0 +1,16 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + count_s = {} + count_t = {} + + for i in range(len(s)): + count_s[s[i]] = 1 + count_s.get(s[i], 0) + count_t[t[i]] = 1 + count_t.get(t[i], 0) + + for c in count_t: + if count_t[c] != count_s.get(c, 0): + return False + return True