diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..f432fb8 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -2,19 +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(1) """ - pass + + dict = {} + for string in strings: + sorted_string = ''.join(sorted(string)) + if sorted_string in dict: + dict[sorted_string].append(string) + else: + dict[sorted_string] = [string] + return_list = [] + for key, value in dict.items(): + return_list.append(value) + return return_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_dict = {} + if nums == []: + return nums + for num in nums: + if num in frequency_dict: + frequency_dict[num] += 1 + else: + frequency_dict[num] = 1 + return_list = [] + for i in range(k): + highest_value = max(frequency_dict, key = frequency_dict.get) + return_list.append(highest_value) + frequency_dict.pop(highest_value) + return return_list def valid_sudoku(table): """ This method will return the true if the table is still @@ -25,5 +49,5 @@ def valid_sudoku(table): Time Complexity: ? Space Complexity: ? """ - pass +