Created
May 30, 2018 05:10
-
-
Save sillypog/4633bc7860feaf8c55c157d8233b9805 to your computer and use it in GitHub Desktop.
Searching a trie of characters for a string
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 characters
def valid_word?("", _), do: true | |
def valid_word?(word, tree) do | |
chars = String.codepoints(word) | |
case check_char(chars, tree) do | |
false -> | |
false | |
_ -> | |
[_ | t] = chars | |
valid_word?(Enum.join(t), tree) | |
end | |
end | |
def check_char(_, :leaf), do: false | |
def check_char([], _), do: true | |
def check_char([h | t], current_node) do | |
case current_node[h] do | |
nil -> | |
true | |
next_node -> | |
check_char(t, next_node) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment