Skip to content

Commit 243d89f

Browse files
committed
add solutions for contains duplicate, two sum, valid anagram
1 parent 7db46d5 commit 243d89f

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

contains-duplicate/JoyJaewon.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Brainstrom
3+
- brute force: double for-loop (O^2)
4+
- efficient approach: use set O(N)
5+
6+
Plan
7+
1. Initialize the set
8+
2. Iterate over the array
9+
2-1. For each element, check if it's in the set. If it is, return True
10+
2-2. If not, add the num to the set
11+
3. Return False since no duplicates are found
12+
'''
13+
14+
def containsDuplicate(nums):
15+
num_set=set()
16+
for num in nums:
17+
if num in num_set:
18+
return True
19+
num_set.add(num)
20+
return False
21+
22+
#TC: O(N), SC: O(N)
23+
24+
#Normal case
25+
print(containsDuplicate([1, 2, 3, 1]) == True)
26+
print(containsDuplicate([1, 2, 3, 4]) == False)
27+
28+
#Edge case
29+
print(containsDuplicate([1]) == False)
30+
print(containsDuplicate([]) == False)

two-sum/JoyJaewon.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Brainstorm
3+
- Brute force: double for-loop / recursion - O(N^2)
4+
- Two Pointer: O(nlogn) - sorting
5+
- Efficient approach: use hashmap to go through the array once O(N)
6+
7+
Plan
8+
1. Initialize the dictionary
9+
2. Iterate over the array
10+
2-1. For each number, calculate the complement
11+
2-2. check if the complement exists in the dictionary
12+
2-3. Return the indices if it exists
13+
2-4. If not, add the current number and its index to the dictionary
14+
'''
15+
16+
def twoSum(nums, target):
17+
memo = {}
18+
for i in range(len(nums)):
19+
diff = target - nums[i]
20+
if diff in memo:
21+
return [memo[diff], i]
22+
memo[nums[i]] = i
23+
24+
#TC:O(N), SC:O(N)
25+
26+
#Normal case
27+
print(twoSum([2, 7, 11, 15], 9) == [0, 1])
28+
print(twoSum([3, 2, 4], 6) == [1, 2])
29+
30+
#Edge case
31+
print(twoSum([1, 2], 3) == [0, 1])
32+
print(twoSum([1, 5, 10], 20) == None)

valid-anagram/JoyJaewon.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Brainstorm
3+
- brute force: double for-loop O(N^2)
4+
- efficient: use hash map O(N)
5+
6+
plan
7+
1. check the string lengths. Return false if different
8+
2. Initialize the dictionary
9+
3. Check the dictionary
10+
3-1. If all counts are zero, strings are anagrams. Return True
11+
3-2. If not, return False
12+
'''
13+
def isAnagram(s, t):
14+
if len(s) != len(t):
15+
return False
16+
17+
count = {}
18+
for char in s:
19+
if char in count:
20+
count[char] += 1
21+
else:
22+
count[char] = 1
23+
24+
for char in t:
25+
if char in count:
26+
count[char] -= 1
27+
if count[char] < 0:
28+
return False
29+
else:
30+
return False
31+
32+
return all(x == 0 for x in count.values())
33+
34+
#TC: O(N), SC: O(N)
35+
36+
#Normal case
37+
print(isAnagram("anagram", "nagaram") == True)
38+
print(isAnagram("rat", "car") == False)
39+
40+
#Edge case
41+
print(isAnagram("", "") == True)
42+
43+

0 commit comments

Comments
 (0)