diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..f74c857 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -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) """ - pass + hash_table = {} + for word in strings: + sort_word = ''.join(sorted(word)) + if sort_word in hash_table: + hash_table[sort_word].append(word) + else: + hash_table[sort_word] = [word] + + return list(hash_table.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: O(nk) + Space Complexity: O(n) """ - pass + frequency_hash = {} + if nums == []: + return nums + + for num in nums: + if num in frequency_hash: + frequency_hash[num] += 1 + else: + frequency_hash[num] = 1 + + return_array = [] + + for i in range(k): + highest_value = max(frequency_hash, key=frequency_hash.get) + return_array.append(highest_value) + frequency_hash.pop(highest_value) + + return return_array def valid_sudoku(table):