Skip to content

Instantly share code, notes, and snippets.

@slofurno
Created April 17, 2017 13:30
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 slofurno/c037cabc3aaaf57d93a7ba0697eab21c to your computer and use it in GitHub Desktop.
Save slofurno/c037cabc3aaaf57d93a7ba0697eab21c to your computer and use it in GitHub Desktop.
defmodule S do
def anagram(xs, ys) do
count(xs) == count(ys)
end
defp count(xs), do: count(xs, %{})
defp count(<<>>, seen), do: seen
defp count(<<x::utf8, rest::binary>>, seen) do
count(rest, Map.put(seen, x, (seen[x] || 0) + 1))
end
def test do
true = anagram("asdf", "fdsa")
false = anagram("aaaa", "aaa")
:ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment