Last active
September 22, 2022 13:59
-
-
Save deepakkumarnd/823b8abf0d7785da47e0ec160757f8b1 to your computer and use it in GitHub Desktop.
Revisions
-
deepakkumarnd revised this gist
Feb 21, 2021 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,13 +11,14 @@ def insert(new_word) if !contains?(word) temp = self prev = self word.each_char do |c| prev = temp i = to_index(c) temp = @hash[i] || (@hash[i] = Trie.new) end prev.value = word end end -
deepakkumarnd revised this gist
Feb 20, 2021 . 1 changed file with 20 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,39 +1,49 @@ class Trie attr_accessor :hash, :value def initialize @hash = Array.new(26) { nil } @value = nil end def insert(new_word) word = new_word.downcase if !contains?(word) temp = self word.each_char do |c| i = to_index(c) temp = @hash[i] || (@hash[i] = Trie.new) end temp.value = word end end def contains?(word) temp = self word.downcase! word.each_char do |c| i = to_index(c) if @hash[i].nil? return false else temp = @hash[i] end end temp.has_word? end def has_word? !value.nil? end def to_index(c) c.ord - 'a'.ord end end -
deepakkumarnd created this gist
Feb 20, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ class Trie attr_accessor :hash, :word def initialize @hash = [] @word = nil end def insert(new_word) if !contains?(new_word) temp = self new_word.each_char do |c| temp = @hash[c.ord] || (@hash[c.ord] = Trie.new) end temp.word = new_word end end def contains?(word) temp = self word.each_char do |c| if @hash[c.ord].nil? return false else temp = @hash[c.ord] end end temp.has_word? end def has_word? !word.nil? end end trie = Trie.new p trie.insert("parrot") p trie.insert("google") p trie.contains?("facebook") p trie.contains?("answer") p trie.contains?("googl") p trie.insert("monogram") p trie.contains?("parrot")