Skip to content

Commit

Permalink
Merge pull request neetcode-gh#493 from TedTran2019/ruby-solutions
Browse files Browse the repository at this point in the history
Ruby - 208
  • Loading branch information
TedTran2019 authored Jul 15, 2022
2 parents 78e2060 + a714949 commit e8fdc3d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ruby/208-Implement-Trie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Trie
END_OF_WORD = 'END'

def initialize
@root = {}
end

def insert(word)
curr = @root
word.each_char do |char|
curr[char] ||= {}
curr = curr[char]
end
curr[END_OF_WORD] = true
nil
end

def search(word)
curr = @root
word.each_char do |char|
return false unless curr[char]

curr = curr[char]
end
!!curr[END_OF_WORD]
end

def starts_with(prefix)
curr = @root
prefix.each_char do |char|
return false unless curr[char]

curr = curr[char]
end
true
end
end

0 comments on commit e8fdc3d

Please sign in to comment.