diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..57b1548 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -2,18 +2,35 @@ 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 + anagrams_map = {} + for word in strings: + alpha_word = "".join(sorted(word)) + if anagrams_map.get(alpha_word): + anagrams_map[alpha_word].append(word) + else: + anagrams_map[alpha_word]= [word] + + + return list(anagrams_map.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(n) + Space Complexity: O(n) """ - pass + nums_map = {} + + for num in nums: + if not nums_map.get(num): + nums_map[num] = 0 + nums_map[num] += 1 + + sorted_nums_map_keys = sorted(nums_map.keys(), key=nums_map.get, reverse = True ) + return sorted_nums_map_keys[:k] def valid_sudoku(table): @@ -25,5 +42,7 @@ def valid_sudoku(table): Time Complexity: ? Space Complexity: ? """ - pass + is_valid_sodoku = False + + return is_valid_sodoku