Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Space - Katie #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
40 changes: 34 additions & 6 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@

# 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) where m is the num words and n is longest num of characters in the words
# Space Complexity: O(n)

def grouped_anagrams(strings)
raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if strings.empty?
hash_of_letter_groups_and_words = {}
strings.each do |word|
sorted_word = word.chars.sort
if hash_of_letter_groups_and_words.keys.include?(sorted_word)
hash_of_letter_groups_and_words[sorted_word].push(word)
else
hash_of_letter_groups_and_words[sorted_word] = [word]
end
end
return hash_of_letter_groups_and_words.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)
def top_k_frequent_elements(list, k)
raise NotImplementedError, "Method hasn't been implemented yet!"
return [] if list.empty?
hash_occurences = {}
list.each do |num|
if hash_occurences.keys.include?(num)
hash_occurences[num] += 1
else
hash_occurences[num] = 1
end
end

sorted_array = hash_occurences.sort_by{|k,v| -v}

k_most_frequent_elements = []

k.times do |i|
k_most_frequent_elements.push(sorted_array[i][0])
end

return k_most_frequent_elements
end


Expand Down