Solving Anagrams Programmatically: Hash Maps, Sorting, and Combinatorial Explosions
"Astronomer" is an anagram of "moon starer." "Listen" rearranges to "silent." "Dormitory" becomes "dirty room." These feel like they must be intentional, but they're statistical inevitabilities -- ...

Source: DEV Community
"Astronomer" is an anagram of "moon starer." "Listen" rearranges to "silent." "Dormitory" becomes "dirty room." These feel like they must be intentional, but they're statistical inevitabilities -- with 26 letters and enough words in the English language, meaningful anagram pairs emerge by sheer combinatorial volume. But here's the interesting question: how do you find them algorithmically? Given an arbitrary string, how do you efficiently find all valid English words that can be formed from its letters? This is a classic computer science problem that touches on hashing, sorting, tries, and combinatorial complexity. The brute force approach (and why it fails) The most obvious approach: generate all permutations of the input letters and check each one against a dictionary. function* permutations(arr) { if (arr.length <= 1) { yield arr; return; } for (let i = 0; i < arr.length; i++) { const rest = [...arr.slice(0, i), ...arr.slice(i + 1)]; for (const perm of permutations(rest)) { yi