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
34 changes: 28 additions & 6 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,40 @@
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(nlogn)? Not sure on this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Time complexity will be O(n) or O(n*m), where n is the length of strings and m is the length of the longest element in strings since sorted is an O(m) operation. However, if we assume that all words in strings are valid English words, we can reduce the time complexity down to O(n) as m will be relatively small

Space Complexity: 0(n)
"""
pass
anagrams_dict = {}

for word in strings:
sorted_string = ''.join(sorted(word))

if sorted_string in anagrams_dict:
anagrams_dict[sorted_string].append(word)
else:
anagrams_dict[sorted_string] = [word]

return list(anagrams_dict.values())

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: 0(n)
Space Complexity: 0(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
pass
nums_dict = {}

if not nums:
return []

for num in nums:
if num in nums_dict:
nums_dict[num] += 1
else:
nums_dict[num] = 1

most_common_list = sorted(nums_dict, key=nums_dict.get, reverse=True)
return most_common_list[:k]


def valid_sudoku(table):
Expand Down