diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..d10d187 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -1,11 +1,23 @@ +from array import array +from distutils.log import error +import 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 + anagram_dict = {} + for sorted_word in strings: + a = tuple(sorted(sorted_word)) + if a in anagram_dict: + anagram_dict[a].append(sorted_word) + else: + anagram_dict[a] = [sorted_word] + return list(anagram_dict.values()) def top_k_frequent_elements(nums, k): """ This method will return the k most common elements @@ -13,7 +25,24 @@ def top_k_frequent_elements(nums, k): Time Complexity: ? Space Complexity: ? """ - pass + if len(nums) == 0: + return [] + + count = {} + freq = [[] for i in range(len(nums) + 1)] + + for n in nums: + count[n] = 1 + count.get(n, 0) + + for n, c in count.items(): + freq[c].append(n) + + res = [] + for i in range(len(freq) - 1, 0, -1): + for n in freq[i]: + res.append(n) + if len(res) == k: + return res def valid_sudoku(table): @@ -26,4 +55,4 @@ def valid_sudoku(table): Space Complexity: ? """ pass - +print(top_k_frequent_elements([1, 2, 2, 2, 3, 3, 3], 2)) \ No newline at end of file