diff --git a/lib/exercises.rb b/lib/exercises.rb index e1b3850..5a61f0c 100644 --- a/lib/exercises.rb +++ b/lib/exercises.rb @@ -1,21 +1,76 @@ # 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) +# Space Complexity: O(n) + +# hash function that assigns each letter a value +def alphabet_keys() + alphabet = "abcdefghijklmnopqrstuvwxyz".split('') + alphabet_keys = {} + alphabet.length.times do |i| + alphabet_keys[alphabet[i]] = i + 1 + end + + return alphabet_keys +end def grouped_anagrams(strings) - raise NotImplementedError, "Method hasn't been implemented yet!" + return [] if strings == [] + + alphabet_keys = alphabet_keys() + string_keys = {} + + # iterate through each string + strings.each do |str| + key = 0 + # use alphabet_keys to assign each word a key by adding together values of each letter in the word + str.split('').each do |l| + key += alphabet_keys[l] + end + # if key exists, add word to that key + if string_keys[key] + string_keys[key].push(str) + else # otherwise create a new key-value pair + string_keys[key] = [str] + end + end + + grouped_anagrams = [] + string_keys.each do |k, v| + grouped_anagrams.push(v) + end + + return grouped_anagrams 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) +# Space Complexity: O(n) def top_k_frequent_elements(list, k) - raise NotImplementedError, "Method hasn't been implemented yet!" -end + return [] if list.empty? + frequency = {} + list.each do |i| + frequency[i] ? (frequency[i] += 1) : (frequency[i] = 1) + end + + nums = [] + unique = list.uniq + max = frequency.values.max + until max == 0 || nums.length == k + unique.each do |i| + break if nums.length == k + if frequency[i] == max + nums << i + end + end + max -= 1 + end + + return nums +end # This method will return the true if the table is still # a valid sudoku table. diff --git a/test/exercises_test.rb b/test/exercises_test.rb index dd93104..0ba60c9 100644 --- a/test/exercises_test.rb +++ b/test/exercises_test.rb @@ -81,6 +81,18 @@ expect(answer.sort).must_equal [1,2] end + it "works with example 1.5" do + # Arrange + list = [2,1,1,2,2,3] + k = 2 + + # Act + answer = top_k_frequent_elements(list, k) + + # Assert + expect(answer).must_equal [2,1] + end + it "works with example 2" do # Arrange list = [1]