diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..7aaa9d7 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,19 +1,54 @@ # 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) - n is number of words in the array +# Space Complexity: O(n) - n is number of words in the array def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if strings.empty? + + hash = {} + + strings.each do |word| + sorted_word = word.chars.sort.join + if hash[sorted_word] + hash[sorted_word] << word + else + hash[sorted_word] = [] + hash[sorted_word] << word + end + end + + return 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(n) - n is length of list +# Space Complexity: O(n) - n is the length of the list def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if list.empty? + return list if list.size == 1 + return [list[0]] if k == 1 + + hash = {} + list.each do |num| + if hash[num] + hash[num] += 1 + else + hash[num] = 1 + end + end + + sorted_nums = hash.sort_by{|num,count| count}.reverse + + most_freq = [] + i = 0 + k.times do + most_freq << sorted_nums[i][0] + i+=1 + end + + return most_freq end