anagram
This commit is contained in:
21
elixir/anagram/lib/anagram.ex
Normal file
21
elixir/anagram/lib/anagram.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule Anagram do
|
||||
@doc """
|
||||
Returns all candidates that are anagrams of, but not equal to, 'base'.
|
||||
"""
|
||||
@spec match(String.t(), [String.t()]) :: [String.t()]
|
||||
def match(base, candidates) do
|
||||
candidates
|
||||
|> Enum.filter(&anagram?(normalize(base), normalize(&1)))
|
||||
end
|
||||
|
||||
defp anagram?(base, base), do: false
|
||||
defp anagram?(base, candidate), do: hash(base) == hash(candidate)
|
||||
|
||||
@spec normalize(String.t()) :: String.t()
|
||||
defp normalize(string), do: string |> String.downcase() |> String.graphemes()
|
||||
|
||||
@spec hash(String.t()) :: Map.t()
|
||||
defp hash(string) do
|
||||
Enum.reduce(string, %{}, fn char, acc -> Map.update(acc, char, 1, &(&1 + 1)) end)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user