Anagram Problem in Elixir

Search for a command to run...

No comments yet. Be the first to comment.
Phoenix Channels enable developers to build scalable, real-time applications in Elixir. Backed by the concurrency power of the BEAM, Channels allow you to create chat apps, live dashboards, games, and more with low latency and high throughput. In thi...

Elixir is increasingly becoming a go-to choice for companies that need scalable, concurrent, and fault-tolerant systems. Built on the rock-solid Erlang VM (BEAM), Elixir brings together productivity and performance, making it ideal for modern web and...

When working with Phoenix, the web framework built on Elixir, you’ll often find yourself defining schemas to map your database tables to Elixir structs. These schemas are powerful tools that help manage data interactions in a clean, structured way. B...

Phoenix LiveView is a groundbreaking library for Elixir that enables the creation of rich, real-time web interfaces without the need for complex JavaScript frameworks. Built on top of the Phoenix framework, LiveView leverages Elixir's strengths in co...

An anagram is a rearrangement of letters to form a new word. For example, "owns" is an anagram of "snow". An interesting twist is that a word is not considered its own anagram. In this article, we will explore how to solve the anagram problem using Elixir.
Given a target word and a list of candidate words, we need to find the subset of candidates that are anagrams of the target word. The conditions are:
Lowercase and uppercase characters are equivalent, but the case of the letters should match the original case in the candidate set.
A word is not considered its own anagram.
For example, given the target word "stone" and the candidates ["stone", "tones", "banana", "tons", "notes", "Seton"], the anagram set is ["tones", "notes", "Seton"].
To solve this problem, we'll follow these steps:
Normalize the target word by sorting its characters.
Iterate over the list of candidate words.
Normalize each candidate word by sorting its characters.
Compare the normalized candidate word with the normalized target word.
Collect candidates that match and are not identical to the target word.
Here's how we can implement this solution in Elixir:
We normalize a word by converting it to lowercase and sorting its characters.
def normalize(word) do
word
|> String.downcase()
|> String.graphemes()
|> Enum.sort()
|> Enum.join()
end
We iterate over the list of candidates and collect those that match the normalized target word, excluding the target word itself.
defmodule Anagram do
def find_anagrams(target, candidates) do
normalized_target = normalize(target)
candidates
|> Enum.filter(fn candidate ->
candidate_normalized = normalize(candidate)
candidate_normalized == normalized_target and String.downcase(candidate) != String.downcase(target)
end)
end
defp normalize(word) do
word
|> String.downcase()
|> String.graphemes()
|> Enum.sort()
|> Enum.join()
end
end
Let's test our solution with the given example.
target = "stone"
candidates = ["stone", "tones", "banana", "tons", "notes", "Seton"]
anagrams = Anagram.find_anagrams(target, candidates)
IO.inspect(anagrams) # Output should be ["tones", "notes", "Seton"]
Normalization: The normalize/1 function converts the word to lowercase, splits it into characters, sorts them, and joins them back into a string. This helps in comparing two words irrespective of their original case or order of characters.
Filtering Candidates: The find_anagrams/2 function iterates over each candidate, normalizes it, and checks if it matches the normalized target word. It also ensures that the candidate is not the same as the target word by comparing their lowercase forms.
The anagram problem can be efficiently solved in Elixir by leveraging its powerful string manipulation and enumeration capabilities. The approach of normalizing words by sorting their characters simplifies the comparison process and ensures that we can accurately identify anagrams. This solution is both concise and effective, demonstrating the strengths of Elixir for text processing tasks.
If you're interested in more Elixir tutorials and real-time web development solutions, visit ElixirMasters for expert development services, developer hiring solutions, and consultancy. Elevate your project with the power of Elixir today!