From e6280ae6d7f8742477cc7b09c5c6a69a2cff0d3c Mon Sep 17 00:00:00 2001 From: kaitlyngore Date: Thu, 14 Jul 2022 20:59:13 -0400 Subject: [PATCH 1/2] add anagram and frequent_elements functions --- hash_practice/exercises.py | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..afb190a 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -2,18 +2,44 @@ 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: + Space Complexity: O(n) """ - pass + string_dict = {} + for string in strings: + key = ''.join(sorted(string)) + + if key in string_dict.keys(): + string_dict[key].append(string) + else: + string_dict[key] = [string] + + return list(string_dict.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) + O(n log n)?? + Space Complexity: O(n) """ - pass + frequency_map = {} + return_list = [] + + for num in nums: + if num in frequency_map: + frequency_map[num] += 1 + else: + frequency_map[num] = 1 + + max_values = sorted(frequency_map.values(), reverse=True) + + for key, value in frequency_map.items(): + if value in max_values[:k] and (len(return_list) < k): + return_list.append(key) + + return return_list + + def valid_sudoku(table): From 305078d67315e8e827df41f2af3fce91081dabdd Mon Sep 17 00:00:00 2001 From: kaitlyngore Date: Thu, 14 Jul 2022 22:07:15 -0400 Subject: [PATCH 2/2] almost complete sudoku --- hash_practice/exercises.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index afb190a..0b5b660 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -2,7 +2,7 @@ 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: + Time Complexity: O(n^2) Space Complexity: O(n) """ string_dict = {} @@ -48,8 +48,38 @@ def valid_sudoku(table): Each element can either be a ".", or a digit 1-9 The same digit cannot appear twice or more in the same row, column or 3x3 subgrid - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n^2) + Space Complexity: O(n) """ - pass + # check if the input has too many rows + if len(table) > 9: + return False + for i in range(len(table)): + row_dictionary = {} + column_dictionary = {} + subgrid_dictionary = {} + + # check if the input has too many columns + if len(table[i]) > 9: + return False + + for j in range(len(table[i])): + # check row + if table[i][j] != "." and table[i][j] in row_dictionary: + return False + row_dictionary[table[i][j]] = "yay!" + + # check column + if table[j][i] != "." and table[j][i] in column_dictionary: + return False + column_dictionary[table[j][i]] = "yay!" + + # check sub-grid + # ???? + return True + + + + +