Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite8fdc3d

Browse files
authored
Merge pull requestneetcode-gh#493 from TedTran2019/ruby-solutions
Ruby - 208
2 parents78e2060 +a714949 commite8fdc3d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎ruby/208-Implement-Trie.rb‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
classTrie
2+
END_OF_WORD='END'
3+
4+
definitialize
5+
@root={}
6+
end
7+
8+
definsert(word)
9+
curr=@root
10+
word.each_chardo |char|
11+
curr[char] ||={}
12+
curr=curr[char]
13+
end
14+
curr[END_OF_WORD]=true
15+
nil
16+
end
17+
18+
defsearch(word)
19+
curr=@root
20+
word.each_chardo |char|
21+
returnfalseunlesscurr[char]
22+
23+
curr=curr[char]
24+
end
25+
!!curr[END_OF_WORD]
26+
end
27+
28+
defstarts_with(prefix)
29+
curr=@root
30+
prefix.each_chardo |char|
31+
returnfalseunlesscurr[char]
32+
33+
curr=curr[char]
34+
end
35+
true
36+
end
37+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp