Skip to content

Commit 8775c4a

Browse files
committed
Day 1 final practice code update
1 parent ebb55f5 commit 8775c4a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

leetcode-master/arrays_and_hashing/two_sum.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def twoSum(self, nums, target):
5959
Beats 65.07% of users with Python3
6060
"""
6161

62+
6263
# Solution 2 : One Pass Hash Table
6364
class Solution_2:
6465
def twoSum(self, nums, target):
@@ -73,7 +74,7 @@ def twoSum(self, nums, target):
7374

7475
obj = Solution_2()
7576
ans = obj.twoSum([2, 7, 11, 15], 9)
76-
print(ans)
77+
# print(ans)
7778

7879
"""
7980
================= Results ====================================
@@ -84,4 +85,22 @@ def twoSum(self, nums, target):
8485
Memory
8586
18.12 MB
8687
Beats 6.37% of users with Python3
87-
"""
88+
"""
89+
90+
91+
# Solution 3 :
92+
93+
94+
class Solution_3:
95+
def twoSum(nus, target):
96+
cleaned = []
97+
for i in nus:
98+
if i <= target:
99+
cleaned.append(i)
100+
for i in cleaned:
101+
if target-cleaned[i] in cleaned:
102+
return [nus.index(cleaned[i]), nus.index(target-cleaned[i])]
103+
104+
105+
sol_3 = Solution_3.twoSum(nus = [1, 2, 3, 6], target = 10)
106+
print(sol_3)

leetcode-master/arrays_and_hashing/valid_anagram.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,19 @@ def isAnagram(self, s: str, t: str) -> bool:
126126
45ms Beats 76.79%
127127
16.94MB Beats 43.51%
128128
"""
129+
130+
131+
132+
from collections import defaultdict
133+
class Solution5:
134+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
135+
anagram_dict = defaultdict(list)
136+
result = []
137+
138+
for s in strs:
139+
sorted_s = tuple(sorted(s))
140+
anagram_dict[sorted_s].append(s)
141+
142+
return list(anagram_dict.values())
143+
144+
# https://leetcode.com/problems/group-anagrams/submissions/1455490795

0 commit comments

Comments
 (0)