Skip to content

Commit 0c26402

Browse files
committed
Add solutions of some easy and medium problems from the 'Arrays & Hashing' section.
1 parent 8ce97a7 commit 0c26402

8 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
freq = {}
4+
5+
for num in nums:
6+
current_freq = freq.get(num, 0)
7+
freq[num] = current_freq + 1
8+
if(freq[num] > 1):
9+
return True
10+
11+
return False

Arrays & Hashing/Easy/two_sum.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
idx_dict = {}
4+
5+
for i in range(0, len(nums)):
6+
needed_value = target - nums[i]
7+
if needed_value in idx_dict:
8+
return [i, idx_dict[needed_value]]
9+
idx_dict[nums[i]] = i
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(s) != len(t):
4+
return False
5+
elif sorted(s) == sorted(t):
6+
return True
7+
8+
return False
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
anagrams = [ ]
4+
sortedAnagrams = [ ]
5+
6+
for i in range(0, len(strs)):
7+
sortedString = sorted(strs[i])
8+
if i == 0:
9+
anagrams.append( [strs[i]] )
10+
sortedAnagrams.append(sortedString)
11+
else:
12+
hasFoundGroup = False
13+
for j in range(0, len(sortedAnagrams)):
14+
if sortedString == sortedAnagrams[j]:
15+
hasFoundGroup = True
16+
anagrams[j].append(strs[i])
17+
break
18+
if hasFoundGroup == False:
19+
anagrams.append( [strs[i]] )
20+
sortedAnagrams.append(sortedString)
21+
22+
return anagrams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from collections import defaultdict
2+
3+
class Solution:
4+
def dfs(self, vertex: int, graph: defaultdict, visited: defaultdict, componentSize: int) -> None:
5+
visited[vertex] = True
6+
componentSize[0] += 1
7+
8+
for neighbor in graph[vertex]:
9+
if(neighbor not in visited):
10+
self.dfs(neighbor, graph, visited, componentSize)
11+
12+
return
13+
14+
def longestConsecutive(self, nums: List[int]) -> int:
15+
n = len(nums)
16+
graph = { }
17+
visited = { }
18+
19+
for i in range(0, n):
20+
vertex = nums[i]
21+
if vertex in graph:
22+
continue
23+
graph[vertex] = []
24+
if(vertex - 1 in graph):
25+
graph[vertex].append(vertex - 1)
26+
graph[vertex - 1].append(vertex)
27+
if(vertex + 1 in graph):
28+
graph[vertex].append(vertex + 1)
29+
graph[vertex + 1].append(vertex)
30+
31+
answer = 0
32+
for i in range(0, n):
33+
vertex = nums[i]
34+
if(vertex in visited):
35+
continue
36+
current = [0]
37+
self.dfs(vertex, graph, visited, current)
38+
answer = max(answer, current[0])
39+
40+
return answer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Non-Optimal Solution - Runtime Complexity: O(n²)
2+
3+
class Solution:
4+
def productExceptSelf(self, nums: List[int]) -> List[int]:
5+
n = len(nums)
6+
answer = [1 for i in range(0, n)]
7+
8+
for i in range(0, n):
9+
product = 1
10+
for j in range(0, n):
11+
if i != j:
12+
product *= nums[j]
13+
answer[i] = product
14+
15+
return answer
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
freqDict = { }
4+
5+
for num in nums:
6+
freqDict[num] = freqDict.get(num, 0) + 1
7+
8+
kvPairs = [(value, key) for key, value in freqDict.items()]
9+
kvPairs = sorted(kvPairs)
10+
kvPairs.reverse()
11+
kvPairs = kvPairs[ : k]
12+
answer = [pair[1] for pair in kvPairs]
13+
14+
return answer
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def isValidSudoku(self, board: List[List[str]]) -> bool:
3+
digits = [str(i) for i in range(1, 10)]
4+
5+
# Checking each row
6+
for i in range(0, len(board)):
7+
freq = { }
8+
for j in range(0, len(board)):
9+
if(board[i][j] in digits):
10+
freq[board[i][j]] = freq.get(board[i][j], 0) + 1
11+
if(freq[board[i][j]] > 1):
12+
return False
13+
14+
# Checking each column
15+
for i in range(0, len(board)):
16+
freq = { }
17+
for j in range(0, len(board)):
18+
if(board[j][i] in digits):
19+
freq[board[j][i]] = freq.get(board[j][i], 0) + 1
20+
if(freq[board[j][i]] > 1):
21+
return False
22+
23+
# Checking each 3x3 sub-grid
24+
for i in range(0, len(board), 3):
25+
for j in range(0, len(board), 3):
26+
freq = { }
27+
for x in range(0, 3):
28+
for y in range(0, 3):
29+
if(board[i + x][j + y] in digits):
30+
freq[board[i + x][j + y]] = freq.get(board[i + x][j + y], 0) + 1
31+
if(freq[board[i + x][j + y]] > 1):
32+
return False
33+
34+
return True

0 commit comments

Comments
 (0)