Skip to the content

The anagrams of a word

Problem

Two words are anagrams when they use exactly the same letters, each one the same number of times, even if the order is different: "listen" and "inlets". Write a function that takes a word and a list of candidates, and returns a list with the candidates that are anagrams of the word.

The rules: uppercase does not count, so "Carthorse" is an anagram of "orchestra". Letters left over or missing do not pass: "goody" is not an anagram of "good". And a word is not an anagram of itself, so "banana" does not make it into the result for "BANANA".

Hand back the candidates in the same order they arrived and written the way they arrived, with their uppercase letters. If none of them work, return an empty list.

Examples

  • None of them work

    "diaper", ["hello", "world", "zombies", "pants"] → []

  • Two anagrams

    "master", ["stream", "pigeon", "maters"] → ["stream", "maters"]

  • Nothing over, nothing missing

    "good", ["dog", "goody"] → []

  • One among several

    "listen", ["enlists", "google", "inlets", "banana"] → ["inlets"]

  • Same letters, other counts

    "tapper", ["patter"] → []

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def anagrams_of(word, options):
    pass

JavaScript

function anagramsOf(word, options) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →