Skip to content

Commit 849ad6f

Browse files
Merge pull request #364 from coloryourlife/main
2 parents 52f46f4 + d05c695 commit 849ad6f

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
# T: O(N)
9+
# S: O(N)
10+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
11+
# preorder : root - left - right
12+
# inorder : left - root - right
13+
if not preorder and not inorder:
14+
return None
15+
16+
root = TreeNode(preorder[0])
17+
mid = inorder.index(preorder[0])
18+
19+
root.left = self.buildTree(preorder[1 : mid + 1], inorder[:mid])
20+
root.right = self.buildTree(preorder[mid + 1 :], inorder[mid+1:])
21+
22+
return root
23+

counting-bits/coloryourlife.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# T: O(N)
2+
# S: O(N)
3+
class Solution:
4+
def countBits(self, n: int) -> List[int]:
5+
res = []
6+
for i in range(n + 1):
7+
m = i
8+
cnt = 0
9+
while m > 0:
10+
if m & 1 == 1:
11+
cnt += 1
12+
m = m >> 1
13+
res.append(cnt)
14+
return res
15+

decode-ways/coloryourlife.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
# T: O(N)
3+
# S: O(N)
4+
def numDecodings(self, s: str) -> int:
5+
# The subproblem would be any portion of string
6+
dp = {len(s): 1}
7+
8+
def dfs(i):
9+
if i in dp:
10+
return dp[i]
11+
if s[i] == "0":
12+
return 0
13+
14+
res = dfs(i + 1)
15+
if i + 1 < len(s) and (
16+
s[i] == "1" or s[i] == "2" and s[i + 1] in "0123456"
17+
):
18+
res += dfs(i + 2)
19+
dp[i] = res
20+
return res
21+
22+
return dfs(0)
23+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Codec:
2+
# T: O(n)
3+
# S: O(1)
4+
def encode(self, strs: List[str]) -> str:
5+
"""Encodes a list of strings to a single string.
6+
"""
7+
res = ""
8+
for s in strs:
9+
res += str(len(s)) + "#" + s
10+
return res
11+
12+
13+
def decode(self, s: str) -> List[str]:
14+
"""Decodes a single string to a list of strings.
15+
"""
16+
res, i = [], 0
17+
while i < len(s):
18+
j = i
19+
while s[j] != "#":
20+
j += 1
21+
length = int(s[i:j])
22+
res.append(s[j + 1 : j + 1 + length])
23+
i = j + 1 + length
24+
return res
25+

valid-anagram/coloryourlife.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
# 1. sorting and compare : O(nlogn)
4+
# 2. hashing: O(n)
5+
# 3. array: O(n)
6+
if len(s) != len(t):
7+
return False
8+
char_dict = defaultdict(int)
9+
for c in s:
10+
char_dict[c] += 1
11+
12+
for c in t:
13+
char_dict[c] -= 1
14+
if char_dict[c] < 0:
15+
return False
16+
17+
return True
18+

0 commit comments

Comments
 (0)