Arrays and strings · Exercise 8 of 100

Anagrams

Two lowercase words are anagrams if each letter occurs equally often in both words. Counting each letter lets you check whether rearranging the first word can produce the second, even when letters repeat.

Try it yourself

Which word is an anagram of the lowercase word reeds? Choose between steer, seder and seeds.

One-step hint

Tally each letter in reeds, then cross off one tally per letter of a candidate. A match uses every tally exactly once.

Worked solution
  1. Count the letters in reeds. You need two copies of e and one each of r, d and s.
  2. Steer has a t in place of d, and seeds has an extra s in place of r. Only seder has all the required counts.
DSA compiler