Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@

# 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 being the quantity of strings
# Space Complexity: O(1) bc we are only creating one new hash

def grouped_anagrams(strings)
Comment on lines +4 to 7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Method hasn't been implemented yet!"
letter_hash = {}

strings.each do |word|
if letter_hash[word.split("").sort] == nil
letter_hash[word.split("").sort] = [word]
else
letter_hash[word.split("").sort] << word
end
end

return letter_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 log n)
# Space Complexity: O(n)
# not sure why this one is failing a test bc it returns the correct response in repl??
def top_k_frequent_elements(list, k)
Comment on lines +23 to 26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if list == []
hash = {}

list.each do |num|
if hash[num]
hash[num] += 1
else
hash[num] = 1
end
end

sorted = hash.sort_by{|num, frequency| -frequency}

result = []
k.times do |key, value|
result << sorted[key][0]
end

return result
end


Expand Down