↔️

Anagrams

Week 37

Your friend poses an interesting question - "dog" is an anagram of "god" (that is, words that feature exactly the same letters and letter frequencies (note that "good" is NOT considered an anagram of "dog", since it has 2 "o"'s, while "dog" only has 1), but just arranged in a different order) - they are interested in ALL the examples of anagrams in the English language. They will use this both for curiosity and when playing many word games like Scrabble, Wordle, Boggle, Countdown etc. Other examples would be "listen" and "silent", "evil" and "vile", "ogre" and "gore" etc

Suppose you have the following word list:

Your program should take that data and output information in the following format - note:

  • Lines should be ordered in descending order of the anagram count - i.e. in this example of 12 words, we see the letters with 4 anagrams at the top, then 3, 2, 1...
  • Words should be ordered alphabetically within the line - i.e. "angered" is first for the anagrams that can be made with those letters, while "grenade" is last
  • For groups with the same count - e.g. "tycoon", "rollercoaster" and "code" are all in groups containing only 1 word, then they can be displayed in any order - here, the lines go tycoon, rollercoaster, code...but another order like code, rollercoaster, tycoon will also be accepted
  • Include a final count of the total number of anagrams from all words at the end, in the exact format as shown below

Above was just an example - for your program, you should use this word list

Hints

Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts

Release Day Hint
2 There are of course many ways to attempt this challenge - the difficult thing might be determining in an easy/efficient way whether an anagram already exists for the current word. A map (object/dictionary/associative array) might be hard to check for equality, while an array (1-26) of character counts would be sparse and hard to check. A better way might be to split the words by character and sort the letters alphabetically - e.g. both "enraged" and "angered" would be represented as "adeegnr"
3 You can then create an object mapping these string keys with an array of the word anagrams they represent
4 At the end, loop through the object and sort each array of anagrams alphabetically, as well as the object by the anagram array length in descending order
5 You can then loop through the object and output in the desired format - if the anagram count for each key is >= 2, then you can add that count to the total valid anagram count
37 2026