What is an Anagram?
An anagram is a word or phrase formed by rearranging all the letters of another word or phrase, using each letter exactly once. For example, "listen" is an anagram of "silent," "astronomer" is an anagram of "moon starer," and "the classroom" is an anagram of "school master."
Anagrams have been used throughout history for cryptography, literary games, and wordplay. Famous examples include "William Shakespeare" → "I am a weakish speller" and "Clint Eastwood" → "Old West action." In the modern era, anagrams appear in word games like Scrabble, crossword puzzles, and literary puzzles.
Detecting anagrams algorithmically is simple: sort both strings alphabetically (ignoring spaces and case) and check if they are identical. If yes, they are anagrams. This O(n log n) approach works for any length strings.