We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 53c3815 commit c1f1d5eCopy full SHA for c1f1d5e
ruby/208-Implement-Trie.rb
@@ -0,0 +1,38 @@
1
+class Trie
2
+ END_OF_WORD = 'END'
3
+
4
+ def initialize
5
+ @root = {}
6
+ end
7
8
+ def insert(word)
9
+ curr = @root
10
+ last_idx = word.length - 1
11
+ word.each_char.with_index do |char, idx|
12
+ curr[char] ||= {}
13
+ curr = curr[char]
14
+ curr[END_OF_WORD] = true if last_idx == idx
15
16
+ nil
17
18
19
+ def search(word)
20
21
+ word.each_char do |char|
22
+ return false unless curr[char]
23
24
25
26
+ !!curr[END_OF_WORD]
27
28
29
+ def starts_with(prefix)
30
31
+ prefix.each_char do |char|
32
33
34
35
36
+ true
37
38
+end
0 commit comments