From 50bf1933bbbcc29414dc6a8817095ee21b3857ce Mon Sep 17 00:00:00 2001 From: Ainur Dzhaianbaeva Date: Sat, 25 Jun 2022 15:03:18 -0700 Subject: [PATCH 1/3] solve grouped anagrams problem --- hash_practice/exercises.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..e4bbec2 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -5,7 +5,22 @@ def grouped_anagrams(strings): Time Complexity: ? Space Complexity: ? """ - pass + if not strings: + return [] + + result = {} + + for word in strings: + key = ''.join(sorted(word)) + if key in result: + result.get(key).append(word) + else: + result[key] = [word] + return result.values() + + + + def top_k_frequent_elements(nums, k): """ This method will return the k most common elements From 67c9bf32aa4472bb673673e10917057716c69ae8 Mon Sep 17 00:00:00 2001 From: Ainur Dzhaianbaeva Date: Sat, 25 Jun 2022 16:43:36 -0700 Subject: [PATCH 2/3] solve top_k_frequent_elements problem --- hash_practice/exercises.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index e4bbec2..c9e951b 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -2,14 +2,13 @@ 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(nklog(k)), where k is the length of the string + Space Complexity: O(log(n)) """ if not strings: return [] result = {} - for word in strings: key = ''.join(sorted(word)) if key in result: @@ -28,7 +27,25 @@ def top_k_frequent_elements(nums, k): Time Complexity: ? Space Complexity: ? """ - pass + if not nums: + return [] + count = {} + frequency = [[] for i in range(len(nums)+1)] + for n in nums: + if n not in count: + count[n] = 0 + count[n] += 1 + + for key, val in count.items(): + frequency[val].append(key) + + + result = [] + for i in range(len(frequency)-1, 0, -1): + for n in frequency[i]: + result.append(n) + if len(result) == k: + return result def valid_sudoku(table): From 4db1eeef78fead0183581f859b958d0077bee89a Mon Sep 17 00:00:00 2001 From: Ainur Dzhaianbaeva Date: Sun, 17 Jul 2022 16:13:25 -0700 Subject: [PATCH 3/3] solve validSudoku() function --- hash_practice/exercises.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index c9e951b..01ab9d9 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -24,8 +24,8 @@ def grouped_anagrams(strings): 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) """ if not nums: return [] @@ -54,8 +54,25 @@ 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: ? - """ - pass + Time Complexity: o(n^2) + Space Complexity: O(n) +# """ +from collections import defaultdict +def validSudoku(board): + if board == None: + return None + N = 9 + cols = defaultdict(set) + rows = defaultdict(set) + squares = defaultdict(set) + for row in range(9): + for col in range(9): + if board[row][col] == ".": + continue + if(board[row][col] in rows[row] or board[row][col] in cols[col] or board[row][col] in squares[(row // 3, col // 3)]): + return False + cols[col].add(board[row][col]) + rows[row].add(board[row][col]) + squares[(row // 3, col // 3)].add(board[row][col]) + return True