Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 52 additions & 9 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,72 @@

# 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 * mlogm) where n is the length of the strings array and m is the length of each word

Choose a reason for hiding this comment

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

👍

# since each word is getting sorted
# Space Complexity: O(n) where n is the size of the strings array - worst case there are no anagrams and the
# hash is the same size as the array

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if strings.length == 0

hash = {}

strings.each do |word|
sorted_word = word.chars.sort.join

if !hash[sorted_word]
hash[sorted_word] = [word]
else
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) where n is the length of the list
# Space Complexity: O(n) where n is the length of the lists
def top_k_frequent_elements(list, k)

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!"
end
return [] if list.length == 0

hash = {}
list.each do |ele|
if !hash[ele]
hash[ele] = 1
else
hash[ele] += 1
end
end

top_occurring = []

k.times do |i|
top_val = nil
top_freq = 0

hash.each do |ele, freq|
if freq > top_freq
top_freq = freq
top_val = ele
end
end

hash.delete(top_val)
top_occurring.push(top_val)
end

return top_occurring
end

# This method will return the true if the table is still
# a valid sudoku table.
# Each element can either be a ".", or a digit 1-9
# The same digit cannot appear twice or more in the same
# The same digit cannot appear twice or more in the same
# row, column or 3x3 subgrid
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
end
end
2 changes: 1 addition & 1 deletion test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
end
end

xdescribe "top_k_frequent_elements" do
describe "top_k_frequent_elements" do
it "works with example 1" do
# Arrange
list = [1,1,1,2,2,3]
Expand Down