When I first saw “check if two strings are anagrams,” I reached for sort. Alphabetize both, compare. It works. But there is something wasteful happening that took me too long to notice.
The logic seems airtight: if "listen" and "silent" contain the same characters in different order, sorting both will produce the same string. Compare those sorted strings and you have your answer. This is the approach most people reach for first because sorting feels like it imposes order on chaos -- it transforms a messy comparison into a neat one.
But sorting does more work than the problem demands. To sort a string of length n, you perform O(n log n) comparisons. Each comparison asks “does this character come before that one?” -- a question about ORDER. The anagram problem never asks about order. It asks: “do these two strings contain the same characters in the same quantities?” That is a question about COUNT, not position. Sorting answers a harder question than the one you asked.
Try it yourself. Sort the letters of "listen" into alphabetical order by swapping pairs. Feel every swap. Count them. That tedium is the point -- you are about to do unnecessary work, and the friction is the lesson.
Tap to select, tap another to swap. Or use Enter to select and arrow keys to move.