Two strings are anagrams exactly when their character-count maps are equal.
Problem trace
Anagrams require matching counts, not merely the same distinct letters
Read it this way:Strings aab and abb both have length 3. Their character counts differ, so they are not anagrams. Which character count must change to turn abb into an anagram of aab?
Sources and provenance
Full interview answer 14 min read
Determine whether one string can be rearranged into the other using exactly the same character occurrences.
Problem
Input. Two Python strings, first and second.
Output. Return True if both strings contain each character the same number of times; otherwise return False.
Rules. Order does not matter, but multiplicity does.
Compare exact code points, including case and whitespace, without normalization or filtering.
Do not change either string. Two empty strings match; unequal lengths cannot match.
Example."aab" and "aba" return True because both contain two a characters and one b.
For "aab" and "abb", return False.
Picture: Count Repetitions, Not Just Membership
Problem trace
Separate full strings: build and compare every character count
Input and goalCheck whether two strings contain the same letters with the same counts.
Initialize both countersCounter starts with no entries for either string; the divider separates first="aab" from second="aba".
count firsta0a1b2|3a4b5a6
examplefirst = "aab", second = "aba"mapLabelCounter(first) and Counter(second)
Counter(first) and Counter(second)empty
count first
0
example
first = "aab", second = "aba"
index 0
a
Count the first aReading first[0]="a" changes the left count for a from 0 to 1.
left a: 0 -> 1a0a1b2|3a4b5a6
examplefirst = "aab", second = "aba"
saved statealeft 1 | right 0
key a
not shown→left 1 | right 0
Count the repeated aReading first[1]="a" changes the same left entry from 1 to 2.
a0left a: 1 -> 2a1b2|3a4b5a6
examplefirst = "aab", second = "aba"
saved statealeft 2 | right 0
key a
left 1 | right 0→left 2 | right 0
left a
0→1
Finish the first counterReading first[2]="b" adds b:1, so Counter(first) is {a:2, b:1}.
a0a1left b: 0 -> 1b2|3a4b5a6
examplefirst = "aab", second = "aba"leftCounter{a:2, b:1}
saved statealeft 2 | right 0bleft 1 | right 0
key b
not shown→left 1 | right 0
left b
1→2
left counter
not shown→{a:2, b:1}
Start the second counterCounter(second) is independent. Reading second[0]="a" sets its a count to 1.
a0a1b2|3right a: 0 -> 1a4b5a6
examplefirst = "aab", second = "aba"
saved statealeft 2 | right 1bleft 1 | right 0
key a
left 2 | right 0→left 2 | right 1
right a
2→4
left counter
{a:2, b:1}→not shown
Count b in the second stringReading second[1]="b" sets its b count to 1.
a0a1b2|3a4right b: 0 -> 1b5a6
examplefirst = "aab", second = "aba"
saved statealeft 2 | right 1bleft 1 | right 1
key b
left 1 | right 0→left 1 | right 1
right b
4→5
Finish the second counterReading second[2]="a" changes its a count from 1 to 2, producing {a:2, b:1}.
a0a1b2|3a4b5right a: 1 -> 2a6
examplefirst = "aab", second = "aba"rightCounter{a:2, b:1}
saved statealeft 2 | right 2bleft 1 | right 1
key a
left 2 | right 1→left 2 | right 2
right a
5→6
right counter
not shown→{a:2, b:1}
Compare the complete mapsBoth keys and counts agree: a has 2 on each side and b has 1 on each side, so Counter(first) == Counter(second).
a0a1left completeb2|3a4b5right completea6
examplefirst = "aab", second = "aba"comparison{a:2,b:1} = {a:2,b:1}resulttrue
saved statealeft 2 = right 2bleft 1 = right 1
key a
left 2 | right 2→left 2 = right 2
key b
left 1 | right 1→left 1 = right 1
left complete
not shown→2
Recognize it
Order may differ, but the question asks whether two strings contain exactly the same characters with the same multiplicities.
Keep true
After each Counter scan step, the visible count for a character equals its occurrences in the processed prefix; complete equal maps are necessary and sufficient for anagrams.
Reuse it
Replace irrelevant order with a canonical frequency summary; the same idea supports ransom-note checks, multiset equality, permutation detection, and histogram comparison.
Counter starts with no entries for either string; the divider separates first="aab" from second="aba". Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why would matching the set of letters alone be insufficient to establish an anagram?
Reference drawing
Problem trace
Anagrams require matching counts, not merely the same distinct letters
Execution trace
Input and goalCheck whether two strings contain the same letters with the same counts.
Initialize both countersCounter starts with no entries for either string; the divider separates first="aab" from second="aba".
count firsta0a1b2|3a4b5a6
examplefirst = "aab", second = "aba"mapLabelCounter(first) and Counter(second)
Counter(first) and Counter(second)empty
count first
0
example
first = "aab", second = "aba"
index 0
a
Count the first aReading first[0]="a" changes the left count for a from 0 to 1.
left a: 0 -> 1a0a1b2|3a4b5a6
examplefirst = "aab", second = "aba"
saved statealeft 1 | right 0
key a
not shown→left 1 | right 0
Count the repeated aReading first[1]="a" changes the same left entry from 1 to 2.
a0left a: 1 -> 2a1b2|3a4b5a6
examplefirst = "aab", second = "aba"
saved statealeft 2 | right 0
key a
left 1 | right 0→left 2 | right 0
left a
0→1
Finish the first counterReading first[2]="b" adds b:1, so Counter(first) is {a:2, b:1}.
a0a1left b: 0 -> 1b2|3a4b5a6
examplefirst = "aab", second = "aba"leftCounter{a:2, b:1}
saved statealeft 2 | right 0bleft 1 | right 0
key b
not shown→left 1 | right 0
left b
1→2
left counter
not shown→{a:2, b:1}
Start the second counterCounter(second) is independent. Reading second[0]="a" sets its a count to 1.
a0a1b2|3right a: 0 -> 1a4b5a6
examplefirst = "aab", second = "aba"
saved statealeft 2 | right 1bleft 1 | right 0
key a
left 2 | right 0→left 2 | right 1
right a
2→4
left counter
{a:2, b:1}→not shown
Count b in the second stringReading second[1]="b" sets its b count to 1.
a0a1b2|3a4right b: 0 -> 1b5a6
examplefirst = "aab", second = "aba"
saved statealeft 2 | right 1bleft 1 | right 1
key b
left 1 | right 0→left 1 | right 1
right b
4→5
Finish the second counterReading second[2]="a" changes its a count from 1 to 2, producing {a:2, b:1}.
a0a1b2|3a4b5right a: 1 -> 2a6
examplefirst = "aab", second = "aba"rightCounter{a:2, b:1}
saved statealeft 2 | right 2bleft 1 | right 1
key a
left 2 | right 1→left 2 | right 2
right a
5→6
right counter
not shown→{a:2, b:1}
Compare the complete mapsBoth keys and counts agree: a has 2 on each side and b has 1 on each side, so Counter(first) == Counter(second).
a0a1left completeb2|3a4b5right completea6
examplefirst = "aab", second = "aba"comparison{a:2,b:1} = {a:2,b:1}resulttrue
saved statealeft 2 = right 2bleft 1 = right 1
key a
left 2 | right 2→left 2 = right 2
key b
left 1 | right 1→left 1 = right 1
left complete
not shown→2
Recognize it
Order may differ, but the question asks whether two strings contain exactly the same characters with the same multiplicities.
Keep true
After each Counter scan step, the visible count for a character equals its occurrences in the processed prefix; complete equal maps are necessary and sufficient for anagrams.
Reuse it
Replace irrelevant order with a canonical frequency summary; the same idea supports ransom-note checks, multiset equality, permutation detection, and histogram comparison.
Counter starts with no entries for either string; the divider separates first="aab" from second="aba". Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why would matching the set of letters alone be insufficient to establish an anagram?
Read it this way:Strings aab and abb both have length 3. Their character counts differ, so they are not anagrams. Which character count must change to turn abb into an anagram of aab?
Scan
Successive changes
Finished map
first="aab"
a:1, a:2, b:1
{a:2, b:1}
second="aba"
a:1, b:1, a:2
{a:2, b:1}
After each prefix, its map records exactly the occurrences read so far.
Different counts leave some occurrences unmatched. Equal counts let every occurrence pair with the same character, regardless of position.
No position information is needed for this output.
Expected time is O(n+m) for both string lengths; space is O(k) for distinct code points across them.
These bounds assume ordinary hash-table costs.
Change 1: Supply May Contain Surplus
New task. Can supply provide every character in required?
Picture and change. Keep counts but compare required multiplicities against available ones. Extra supply is now allowed.
Code.
from collections import Counterdef can_construct(required, supply): available = Counter(supply) return all(available[character] >= count for character, count in Counter(required).items())
Check.("aab", "baaac") is True; ("aab", "ab") is False; empty requirements always pass.
Use when. The contract is containment, not equality.
Change 2: Declare a Text-Equivalence Policy
New task. Ignore case and canonical Unicode composition differences.
Picture and change. Apply casefold, then NFC normalization, before counting. This deliberately changes the exact reference.
Code.
from collections import Counterfrom unicodedata import normalizedef is_anagram_folded(first, second): left = normalize("NFC", first.casefold()) right = normalize("NFC", second.casefold()) return Counter(left) == Counter(right)
Check.("Tea", "EAT") passes. ("\u00e9", "e\u0301") also passes here but fails the exact reference.
Use when. The application explicitly selects this equivalence; it is not universal linguistic equality.
Failure: Replacing Counts With a Set
"aab" and "abb" have equal lengths and equal sets, but different multiplicities. A set loses the deciding information.
Transfer Check
Why can equal total lengths not replace the maps?
Reasoned Answer
Length fixes only the sum of counts. Different allocations among characters can have the same sum.
Code Points Are Not Always Visible Characters
"\u00e9" contains one precomposed code point; "e\u0301" contains a letter and a combining mark.
Their visible forms can look alike while their exact counters differ.
Case folding can also change length: "\u00df" folds to "ss".
The normalized variant defines equality after a specific transformation sequence.
It does not remove punctuation, ignore accents, or implement locale-specific or grapheme-cluster policies.
Those would be further task changes requiring explicit definitions and tests.
Counter construction explains the reference’s expected linear bound.
Normalization adds transformation work and storage; do not silently attribute only counter costs to that variant.
The grouping application reuses exact multiplicities as bucket keys under its own alphabet contract.