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
16 changes: 13 additions & 3 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

# 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 * m log m)we go through the array once and for each string we sort which is an m log m time complexcity
# Space Complexity: O(n) worse case we don't have any anagram and the size of the hash table is the same size of the array

def grouped_anagrams(strings)

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 strings.length == 0
word_hash = Hash.new()
strings.each do |string|
element = string.split("").sort.join
if word_hash[element]
word_hash[element] << string
else
word_hash[element] = [string]
end
end
return word_hash.values
end

# This method will return the k most common elements
Expand Down