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
37 changes: 31 additions & 6 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,43 @@
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)

Choose a reason for hiding this comment

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

✨ Nice!

"""
pass
anagram_hash = {}
anagram_list = []

for string in strings:
sorted_string = "".join(sorted(string))

if anagram_hash.get(sorted_string):
anagram_hash[sorted_string].append(string)

else:
anagram_hash[sorted_string] = [string]

for string in anagram_hash.values():
anagram_list.append(string)
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: ?
Time Complexity: O(n log(n))
Space Complexity: O(n)
"""
pass
frequency = {}
for num in nums:
if num not in frequency:
frequency[num] = 1
else:
frequency[num] += 1
sorted_hash = sorted(frequency.items(), key=lambda x:(x[1]), reverse=True)

Choose a reason for hiding this comment

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

🌟 Nice lambda function

most_common = []

for i in sorted_hash:
most_common.append(i[0])
return most_common[:k]


def valid_sudoku(table):
Expand Down