Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
If lengths differ, return false. Otherwise count each letter of s (++) and each of t (−−) in a 26-int array; they're anagrams iff every count ends at 0. O(n).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you compare frequencies in O(n) rather than sorting.
Where candidates slip
No length guard, or defaulting to an O(n log n) sort.
How to stand out
Note the array works for a–z; for Unicode switch to a hash map.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Frequency count (26 array)
1Hint 1Explore hintHide hint
What must be true of the two strings' letter counts?
2Hint 2Explore hintHide hint
Length check first, then a 26-int frequency array.
3Hint 3Explore hintHide hint
++ for s, −− for t; all zero at the end means anagram.
A · Core idea
Two strings are anagrams iff they contain the same letters with the same counts. A 26-slot count array compares frequencies in O(n): increment for each letter of s, decrement for each of t; they match iff every count ends at 0.
Why the length check first. Different lengths can't be anagrams, and the check lets you walk both strings together with one index. It also short-circuits the obvious negatives instantly.
Why ++ for s and −− for t in one array. Using a single array, s adds and t subtracts; equal frequencies cancel to all-zeros. This is neater (and one array) versus building two maps and comparing them.
Why O(1) space. The count array is fixed at 26 regardless of input size (for lowercase a–z). For full Unicode you'd use a hash map instead. Sorting both strings also works but is O(n log n) — the count array is the O(n) answer.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "ab", t = "ba"i=0
+a, −b
a:1, b:-1trueC · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Sorting both strings (O(n log n)) also works, but the count array is the O(n) answer.
ComplexityO(n) time, O(1) space — one pass over both strings (O(n)) with a fixed 26-int array (O(1) space).
E · Edge cases and pitfalls5s = "rat", t = "car" — Counts don't cancel → false.Explore edge casesHide edge cases
Edge cases handled
s = "rat", t = "car"Counts don't cancel → false.s = "a", t = "ab"Length differs → false.s = "", t = ""Both empty → true.
Pitfalls
- Skipping the length check and mishandling unequal lengths.
- Sorting when an O(n) count is expected.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; int[] count = new int[26]; for (int i = 0; i < s.length(); i++) { count[s.charAt(i) - 'a']++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1No length check
Without it, 'abc' vs 'ab' may pass a partial comparison; differing lengths can never be anagrams.
Explore the code comparisonHide the code comparison
No length check
Without it, 'abc' vs 'ab' may pass a partial comparison; differing lengths can never be anagrams.
Don't write this
// straight to counting, no guard
int[] c = new int[26]; ...Write this instead
if (s.length() != t.length()) return false;
int[] c = new int[26]; ...Revise
Carry the pattern into the interview
Pattern to remember
Letter-frequency compare
Code pattern to memorize
len equal? count[s]++ count[t]--; all zero?Rules to remember
- 1Length must match.
- 2++ for s, −− for t.
- 3All zero ⟹ anagram.
Use this pattern when
- Permutation/anagram checks
- Comparing multisets of characters
Interview takeaway
Anagram = equal letter frequencies; a 26-array compares them in O(n).
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Letter-frequency compare. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Group AnagramsComing soonKey each word by its sorted form or count signature.
- Character FrequencyThe frequency map this compares.