Skip to content

Commit b02425a

Browse files
authored
Merge pull request #2 from KyrieJ95/week4
Week4 - 2/5 problems
2 parents c80e2b7 + 34fc9da commit b02425a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

missing-number/joon.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
# Time: O(n)
6+
# Space: O(1)
7+
def missingNumber(self, nums: List[int]) -> int:
8+
n = len(nums)
9+
# MissingNumber = (Sum of 1, 2, ..., n) - Sum of nums)
10+
# Time: O(n)
11+
return n * (n + 1) // 2 - sum(nums)

valid-palindrome/joon.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import re
2+
3+
4+
class Solution:
5+
# Time: O(n)
6+
# Space: O(1)
7+
def isPalindrome(self, s: str) -> bool:
8+
# 1. Convert string
9+
# Time: O(n)
10+
# Space: O(n) since re.sub() will internally use a new string of length n.
11+
s = re.sub('[^a-z0-9]', '', s.lower())
12+
length = len(s)
13+
# 2. Check if the string reads the same forward and backward, one by one.
14+
# Time: O(n)
15+
# Space: O(1)
16+
for i in range(length):
17+
if (s[i] != s[length - 1 - i]):
18+
return False
19+
return True

0 commit comments

Comments
 (0)