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

Commitc1f1d5e

Browse files
committed
208
1 parent53c3815 commitc1f1d5e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎ruby/208-Implement-Trie.rb

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp