From edecefaea2a255ef522f05179aa542fb478f2688 Mon Sep 17 00:00:00 2001 From: Lola Llanes Date: Tue, 5 Jan 2021 00:04:45 -0800 Subject: [PATCH] completed grouped_anagrams and top_k_frequent_elements --- lib/exercises.rb | 58 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..dbffdfd 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,19 +1,65 @@ # 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(m * n) +# Space Complexity: O(n) def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + anagram_hash = {} + + strings.each do |word| + letters = word.chars.sort + sorted_letters = letters.join + + if !anagram_hash.has_key?(sorted_letters) + anagram_hash[sorted_letters] = [word] + else anagram_hash[sorted_letters] << word + end + end + + return anagram_hash.values end # 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(m * n) +# Space Complexity: O(n) +# https://stackoverflow.com/questions/4264133/descending-sort-by-value-of-a-hash-in-ruby +# https://courses.cs.washington.edu/courses/cse373/18au/files/slides/lecture13.pdf + def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + if list.empty? + return list + end + + element_frequency = {} + + list.each do |num| + if element_frequency[num] + element_frequency[num] += 1 + else + element_frequency[num] = 1 + end + end + + frequent_elements = [] + + k.times do + max_frequency = 0 + max_frequent_elements = nil + + element_frequency.each do |key, value| + if value > max_frequency + max_frequent_elements = key + max_frequency = value + end + end + + frequent_elements << max_frequent_elements + element_frequency[max_frequent_elements] = 0 + end + + return frequent_elements end