Anagram algorithm with minimum complexity
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To check whether two strings are anagrams, the minimum practical time complexity is usually linear in the length of the strings, assuming you can count character frequencies efficiently. A sorting-based solution works in O(n log n), but a counting-based solution can reduce the main work to O(n) for the common case.
Define the problem precisely
Two strings are anagrams if they contain the same characters with the same counts, possibly in a different order.
Examples:
- '
listenandsilentare anagrams' - '
rail safetyandfairy talescan be anagrams if spaces are ignored' - '
Listenandsilentare anagrams only if case is normalized'
So before choosing an algorithm, define whether you are ignoring:
- case
- spaces
- punctuation
- Unicode normalization differences
That decision affects both correctness and preprocessing cost.
Sorting-based solution
The simplest widely known algorithm is:
- normalize both strings if needed
- sort both character sequences
- compare the results
This is easy to read, but sorting costs O(n log n).
Counting-based O(n) approach
A more efficient approach counts character frequencies.
This is effectively linear in the length of the input strings, assuming hash operations are well-behaved.
The reasoning is simple:
- if the strings have different lengths, they cannot be anagrams
- if every character count matches, they are anagrams
Manual frequency-array version
If the alphabet is fixed and small, such as lowercase English letters only, you can use an array instead of a hash map.
This is also O(n) and uses constant extra space relative to the fixed alphabet size.
Why O(n) is the real target
You have to inspect every character at least once, so no correct algorithm can beat O(n) time in the general case. That means a counting method is asymptotically optimal for ordinary anagram checking.
So if the question is “minimum complexity,” the practical answer is:
- '
O(n)time with character counting' - '
O(1)extra space for a fixed-size alphabet' - or
O(k)extra space when counting overkdistinct characters
Normalization often matters more than the core algorithm
Real anagram problems are often not about raw letters only. For example:
This kind of preprocessing is often necessary if phrases, punctuation, or case should be ignored.
Choosing the right solution
Use sorting when:
- simplicity matters more than asymptotic optimality
- input sizes are small
- readability is the top priority
Use counting when:
- performance matters
- strings may be large
- you want the asymptotically optimal check
Common Pitfalls
A common mistake is calling a permutation-based approach “the direct solution.” Generating permutations is computationally terrible for this problem.
Another mistake is forgetting to normalize case or spaces when the problem statement expects that.
A third mistake is assuming an O(1) space claim is universal when it really depends on a fixed alphabet assumption.
Summary
- The optimal practical time complexity for anagram checking is
O(n). - Counting character frequencies is better than sorting when performance matters.
- Sorting gives a simple
O(n log n)solution. - Real correctness often depends on normalization rules such as case and whitespace handling.
- Avoid permutation-based solutions; they are drastically less efficient.
Related reading
- Anagram solver based on statistics rather than a dictionary/table?
- Analysis of sorting Algorithm with probably wrong comparator?
- Ant colony optimization using .NET
- Any algorithm for Flip all Light Out game?
- Anatomy of a Memory Leak
- Android - Prevent white screen at startup
- Any pseudo-polynomial algorithm for bounded 0-1 multi-knapsack?
- Any working example of VF2 algorithm?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.