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
45 changes: 39 additions & 6 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@

def create_anagram_key(string):
return ''.join(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*m)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm going to assume that n is the number of words in the list and that by m you mean the number of letters in each word. That being said, note that the call to sorted will actually take O(m * log(m)) time, making this more accurately O(n * m * log(m)).

However, we can make a simplifying assumption since we know that the input list is made up of English words. English words tend not to get long, only about 5 letters per word on average, so the effect of m is going to be dwarfed by the effect of n, since there can easily be hundreds or thousands of words in the list. With that assumption, we can just say the time complexity is just O(n).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes! Where n is the length of strings and m is the length of the longest word in strings. However, if we assume each word in strings is a valid English word, since those are all of a finite length, we can reduce this to say the solution is O(n)

Space Complexity: O(n)
"""
pass
grouped_words = {}
anagrams = []

for ele in strings:
if grouped_words.get(create_anagram_key(ele)) == None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just a minor efficiency thing, if you find yourself calling the same function multiple times with the exact same arguments like you do with create_anagram_key(ele) here, it's usually worth saving the result into a local variable, since every call will recalculate the same data that doesn't change.

grouped_words[create_anagram_key(ele)] = [ele]
else:
grouped_words[create_anagram_key(ele)].append(ele)
Comment on lines +16 to +19

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Instead of calling your helper function three times, consider calling it just once, at the top of the for loop body.

Suggested change
if grouped_words.get(create_anagram_key(ele)) == None:
grouped_words[create_anagram_key(ele)] = [ele]
else:
grouped_words[create_anagram_key(ele)].append(ele)
anagram_key = create_anagram_key(ele)
if grouped_words.get(anagram_key) == None:
grouped_words[anagram_key] = [ele]
else:
grouped_words[anagram_key].append(ele)


for k, v in grouped_words.items():
anagrams.append(v)

return anagrams


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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Don't forget to account for the call to sort on line 42, which will take O(n * log(n)) time and thus be the dominant part of the algorithm for the final time complexity.

Space Complexity: O(n)
Comment on lines +30 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

"""
pass
frequency_map = {}
results = []
for ele in nums:
if ele in frequency_map:
frequency_map[ele] += 1
else:
frequency_map[ele] = 1

values_list = list(frequency_map.values())
values_list.sort(reverse=True)
k_values_list = values_list[:k]

for key, value in frequency_map.items():
if value in k_values_list:
results.append(key)

return results


def valid_sudoku(table):
Expand Down