Skip to content

Commit c9e321d

Browse files
committed
week 1 solution
1 parent f291ae8 commit c9e321d

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

contains-duplicate/i-mprovising.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
O(n) complexity
3+
"""
4+
5+
6+
class Solution:
7+
def containsDuplicate(self, nums: List[int]) -> bool:
8+
nums_set = set(nums)
9+
if len(nums) == len(nums_set):
10+
return False
11+
return True
12+

two-sum/i-mprovising.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
hash table
3+
O(n) complexity
4+
"""
5+
6+
class Solution:
7+
def twoSum(self, nums: List[int], target: int) -> List[int]:
8+
table = {num:idx for idx, num in enumerate(nums)}
9+
for i, x in enumerate(nums):
10+
y = target - x
11+
if (y in table) and table[y] != i:
12+
return [i, table[y]]
13+

valid-anagram/i-mprovising.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
O(n) complexity
3+
"""
4+
from collections import defaultdict
5+
6+
class Solution:
7+
def isAnagram(self, s: str, t: str) -> bool:
8+
s_cnt = defaultdict(int)
9+
t_cnt = defaultdict(int)
10+
for char in s:
11+
s_cnt[char] += 1
12+
for char in t:
13+
t_cnt[char] += 1
14+
if s_cnt != t_cnt:
15+
return False
16+
return True

valid-palindrome/i-mprovising.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
O(n) complexity
3+
"""
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
s = s.lower()
8+
phrase = [c.lower() for c in s if c.isalpha() or c.isdigit()]
9+
10+
if phrase == phrase[::-1]:
11+
return True
12+
return False
13+
14+
15+
16+

valid-parentheses/i-mprovising.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
O(n) complexity
3+
"""
4+
class Solution:
5+
def isValid(self, s: str) -> bool:
6+
open_brackets = ["(", "{", "["]
7+
bracket_map = {"(":")", "{":"}", "[":"]"}
8+
stack = []
9+
for char in s:
10+
if char in open_brackets:
11+
stack.append(char)
12+
continue
13+
if not stack: # no matching open bracket for char
14+
return False
15+
open_b = stack.pop() # last in open bracket
16+
if bracket_map[open_b] != char: # dismatch between open b and close b
17+
return False
18+
if stack: # open bracket remaining
19+
return False
20+
return True
21+
22+

0 commit comments

Comments
 (0)