/
autocomplete.rb
69 lines (54 loc) · 1.47 KB
/
autocomplete.rb
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Coding Exercise: Autocomplete
# Given a list of words and a list of user keyboard actions (keystrokes or WAIT), produce an array of suggested words (sorted lexicographically) for every time a WAIT action is encountered.
# Possible actions are:
# - keystroke (a-zA-Z0-9)
# - WAIT
# - BACKSPACE
# Example:
# Input:
# ****************************
# wordList = ["cats", "cat", "cap", "cape", "cute", "cuts"]
# actions = ["c", "a", "WAIT", "BACKSPACE", "u", "t", "WAIT"]
# Expected Output:
# ****************************
# [["cap", "cape", "cat", "cats"], ["cute", "cuts"]]
# Better answer: use a trie
# https://www.youtube.com/watch?v=zIjfhVPRZCg
class TrieNode
@val = "c"
@children = {c: TrieNode}
@end_of_word = true
end
# c
# |
# a --
# | | |
# p t d
# DFS with stack LIFO
# BFS with queue FIFO
# ['cap', 'cat']
# O(n * m)
def solution(word_list, actions)
string = ''
result = []
word_list.sort!
actions.each do |action|
case action
when 'WAIT'
result << word_list.select { |word| word =~ /^#{string}/ }
when 'BACKSPACE'
string = string[0..-2]
else
string += action
end
end
result
end
describe '#solution' do
it 'returns suggested words based on what user types' do
word_list = ["cats", "cat", "cap", "cape", "cute", "cuts"] # m
actions = ["c", "a", "WAIT", "BACKSPACE", "u", "t", "WAIT"] # n
expect(solution(word_list, actions)).to eq [["cap", "cape", "cat", "cats"],
["cute", "cuts"],]
end
end