From 144fdf26a52df7c97f5a7e5321fdbde7dcf96314 Mon Sep 17 00:00:00 2001 From: Evelynn Kaplan Date: Thu, 12 Sep 2019 15:57:32 -0700 Subject: [PATCH 1/2] started method --- lib/exercises.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 2cb2bfa..69f352b 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,4 +1,4 @@ - +require 'pry' # This method will return an array of arrays. # Each subarray will have strings which are anagrams of each other @@ -6,7 +6,21 @@ # Space Complexity: ? def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + sorted_words = {} + anagrams = [] + + strings.each do |word| + word_sorted = word.chars.sort.join + if !sorted_words.include?(word_sorted) + sorted_words[word_sorted] = sorted_words.length + binding.pry + anagrams[sorted_words.length] = [word] + else + anagrams[sorted_words[word_sorted]].push(word) + end + end + + return anagrams end # This method will return the k most common elements From 4c0a485a6c8d53ae2f1033b16ab1f495148ab444 Mon Sep 17 00:00:00 2001 From: Evelynn Kaplan Date: Sun, 15 Sep 2019 17:29:49 -0700 Subject: [PATCH 2/2] finished grouped_anagrams method --- lib/exercises.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index 69f352b..7b36385 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,9 +1,9 @@ -require 'pry' +require "pry" # 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) where n is the number of strings in the array. +# Space Complexity: O(n) where n is the number of strings in the array. def grouped_anagrams(strings) sorted_words = {} @@ -11,11 +11,13 @@ def grouped_anagrams(strings) strings.each do |word| word_sorted = word.chars.sort.join - if !sorted_words.include?(word_sorted) - sorted_words[word_sorted] = sorted_words.length - binding.pry - anagrams[sorted_words.length] = [word] + if !sorted_words.keys.include?(word_sorted) + # In the hashmap, the sorted word will point to the index in the outer array that the anagram should be placed. + index = sorted_words.length + sorted_words[word_sorted] = index + anagrams[index] = [word] else + # Add anagram to correct group. anagrams[sorted_words[word_sorted]].push(word) end end @@ -31,14 +33,13 @@ def top_k_frequent_elements(list, k) raise NotImplementedError, "Method hasn't been implemented yet!" end - # This method will return the true if the table is still # a 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 +# The same digit cannot appear twice or more in the same # row, column or 3x3 subgrid # Time Complexity: ? # Space Complexity: ? def valid_sudoku(table) raise NotImplementedError, "Method hasn't been implemented yet!" -end \ No newline at end of file +end