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
25 changes: 23 additions & 2 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,36 @@ def grouped_anagrams(strings):
Time Complexity: ?
Space Complexity: ?
"""
pass
anagrams={}
anagram_list=[]
for word in strings:
sorted_word="".join(sorted(word))
if anagrams.get(sorted_word):
anagrams[sorted_word].append(word)
else:
anagrams[sorted_word]=[word]

for word in anagrams.values():
anagram_list.append(word)
return anagram_list

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: ?
"""
pass
freq_dict={}
for num in nums:
if num not in freq_dict:
freq_dict[num]=1
else:
freq_dict[num]+=1
sorted_dict=sorted(freq_dict.items(), key=lambda x:(x[1]), reverse=True)
most_common =[]
for elem in sorted_dict:
most_common.append(elem[0])
return most_common[:k]


def valid_sudoku(table):
Expand Down