forked from AdaGold/hash-practice
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexercises.rb
More file actions
45 lines (39 loc) · 1.41 KB
/
exercises.rb
File metadata and controls
45 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require "pry"
# This method will return an array of arrays.
# Each subarray will have strings which are anagrams of each other
# Time Complexity: O(n) where n is the number of strings in the array.
# Space Complexity: O(n) where n is the number of strings in the array.
def grouped_anagrams(strings)
sorted_words = {}
anagrams = []
strings.each do |word|
word_sorted = word.chars.sort.join
if !sorted_words.keys.include?(word_sorted)
# In the hashmap, the sorted word will point to the index in the outer array that the anagram should be placed.
index = sorted_words.length
sorted_words[word_sorted] = index
anagrams[index] = [word]
else
# Add anagram to correct group.
anagrams[sorted_words[word_sorted]].push(word)
end
end
return anagrams
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)
raise NotImplementedError, "Method hasn't been implemented yet!"
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
# row, column or 3x3 subgrid
# Time Complexity: ?
# Space Complexity: ?
def valid_sudoku(table)
raise NotImplementedError, "Method hasn't been implemented yet!"
end