Skip to content

Instantly share code, notes, and snippets.

@sillypog
Created May 30, 2018 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sillypog/4633bc7860feaf8c55c157d8233b9805 to your computer and use it in GitHub Desktop.
Save sillypog/4633bc7860feaf8c55c157d8233b9805 to your computer and use it in GitHub Desktop.
Searching a trie of characters for a string
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