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
64 changes: 60 additions & 4 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@

# 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)
# Space Complexity: O(n)

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!"
words = {}

while !strings.empty?
i, word_to_compare = 0, strings[0]
words[word_to_compare] = []

while i < strings.length
if permutation(word_to_compare, strings[i])
words[word_to_compare].push(strings[i])
strings.delete_at(i)
i -= 1
end
i += 1
end
end

return words.values
end

def permutation(str1, str2)

Choose a reason for hiding this comment

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

👍

return true if str1.empty? && str2.empty?
return false if str1.nil? || str2.nil?
return false if str1.empty? || str2.empty?

hash = {}

str1.each_char do |char|
hash[char] ? hash[char] += 1 : hash[char] = 1
end

str2.each_char do |char|
hash[char] ? hash[char] -= 1 : false
end

return hash.values.all? { |value| value == 0 }
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: ?
def top_k_frequent_elements(list, k)
Comment on lines 45 to 49

Choose a reason for hiding this comment

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

👍 Time/space complexity?

raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if list.empty? || k == 0

hash, result = [], []

list.each do |num|
hash[num] ? hash[num] += 1 : hash[num] = 1
end

k.times do |time|
max_count, top_occurence = 0, nil

list.each do |num|
if hash[num] > max_count
max_count = hash[num]
top_occurence = num
end
end

result << top_occurence
list -= [top_occurence]
end

return result
end


Expand Down