Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
def sort_string(string):
sorted_chars = sorted(string)
sorted_string = "".join(sorted_chars)

return sorted_string


def grouped_anagrams(strings):
""" This method will return an array of arrays.
Each subarray will have strings which are anagrams of each other
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
"""
pass
arrays = {}
for string in strings:
sorted = sort_string(string)
if sorted in arrays:
arrays[sorted].append(string)
else:
arrays[sorted] = [string]

groups = [group for group in arrays.values()]

return groups


def top_k_frequent_elements(nums, k):
""" This method will return the k most common elements
In the case of a tie it will select the first occuring element.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(nlogn)
Space Complexity: O(n)
"""
pass
if not nums:
return []

freq_map = {}
for e in nums:
if e in freq_map:
freq_map[e] +=1
else:
freq_map[e] = 1

sorted_nums = sorted(freq_map.items(), key=lambda map_key: map_key[1], reverse=True)

common_nums = [e[0] for e in sorted_nums]

return common_nums[:k]


def valid_sudoku(table):
Expand Down