Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Count the character frequencies of both strings. For every letter, the number of deletions needed is the absolute difference between its counts in the two strings. The total minimum deletions to make them anagrams is the sum of those absolute differences over all 26 letters. O(|a| + |b|) time, O(1) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you see that two-sided deletion means summing ALL absolute differences, and can tell it apart from the one-sided Anagram problem where you divide by two.
Where candidates slip
Dividing the summed differences by two out of habit from the Anagram problem, or assuming the two strings must be the same length.
How to stand out
Contrast it explicitly with Anagram: there one replacement fixes both sides at once (so /2); here a deletion only removes from one side, so each surplus is paid independently — no /2.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Interactive decision treeInteractiveChoose the right approachFirst decision previewfreq(a)per letter cYesNoExplore the full treeCollapse the tree
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 2
Two Separate Frequency Maps (Baseline)
1Hint 1Explore hintHide hint
Anagrams share the same multiset. What two structures let you compare multisets directly?
2Hint 2Explore hintHide hint
Build a frequency map for a and another for b.
3Hint 3Explore hintHide hint
For every letter that appears in either map, add the absolute difference of its two counts to the answer.
A · Core idea
Tally a into one frequency map and b into another. Then, for each of the 26 letters, add |fa[c] − fb[c]| to the running total.
Why absolute difference per letter is the deletion count. Consider letter c with fa[c] = 3 and fb[c] = 1. The two strings can only share min(3,1) = 1 copy of c; the other 3 − 1 = 2 copies in a are surplus and must be deleted. In general the surplus on whichever side is larger is |fa[c] − fb[c]|, and that many deletions are unavoidable for that letter. Summed across the alphabet, that's the minimum total.
Why no division by two. Unlike the single-sided Anagram problem, a deletion here only shrinks one string. There is no single operation that simultaneously fixes a's surplus and b's deficit, so each letter's gap is paid in full.
Why two maps is fine but slightly wasteful. Two maps make the logic obvious, which is good for an interview opener. A single signed array fuses both passes and is what you'd ship.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = "cde", b = "abc"Array state
a
c:1, d:1, e:1
4C · Code with teaching comments
Reference implementation previewpublic int makingAnagrams(String a, String b) { int[] fa = new int[26]; int[] fb = new int[26]; for (char c : a.toCharArray()) fa[c - 'a']++; for (char c : b.toCharArray()) fb[c - 'a']++;Explore the full codeHide the full code
D · Why it works and complexityO(|a| + |b|) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
The strings keep only their common sub-multiset min(fa, fb) per letter; everything else is deleted. The deletion count is therefore the size of the symmetric difference of the two histograms — sum(|fa − fb|).
ComplexityO(|a| + |b|) time, O(1) space — Building each map is linear in its string's length. Iterating the 26 possible letters to sum differences is O(1). Two fixed-size maps are O(1) space.
E · Edge cases and pitfalls5a = "abc", b = "abc" — Identical multisets → all diffs 0 → 0 deletions.Explore edge casesHide edge cases
Edge cases handled
a = "abc", b = "abc"Identical multisets → all diffs 0 → 0 deletions.a = "", b = "xyz"Delete all of b → 3 deletions.
Pitfalls
- Iterating only one map's keys, missing letters unique to the other string.
- Dividing by two like the Anagram problem — wrong when both sides can delete.
- Assuming equal lengths and indexing out of bounds.
Approach 2 of 2 · Optimal
Single Signed Histogram (Optimal)
1Hint 1Explore hintHide hint
Do you really need two arrays, or can one signed array track the gap directly?
2Hint 2Explore hintHide hint
Use a single int[26]: add for every character of a, subtract for every character of b.
3Hint 3Explore hintHide hint
After both passes, slot c holds fa[c] − fb[c]. Sum the absolute values of all slots.
A · Core idea
Keep a single int[26] balance. Increment for each character of a, decrement for each character of b. Slot c then equals fa[c] − fb[c]. The answer is Σ |balance[c]|.
Why one array is enough. We never need the two raw counts separately — only their difference per letter. Folding both passes into one signed accumulator gives exactly that difference while halving the bookkeeping.
Why we sum magnitudes, not the raw values. A slot can be positive (a has more) or negative (b has more); both directions represent real deletions, just from different strings. The magnitude |balance[c]| is the number of deletions for that letter regardless of which side is heavier.
Why this differs from Anagram by exactly the /2. In the single-sided Anagram problem, one replacement fixes a surplus in one half and the matching deficit in the other at once, so you count each pair once (sum of positives = sum(|diff|)/2). Here, deletions are one-sided: removing a surplus c from a does nothing for b's separate surplus of some other letter. So every letter's gap is paid in full — Σ |balance|, no division.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = "showman", b = "woman"Array state
after add a
s,h,o,w,m,a,n = +1
2C · Code with teaching comments
D · Why it works and complexityO(|a| + |b|) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Two problems, one histogram, opposite arithmetic: 'replace in one half' → sum positives (=sum|diff|/2); 'delete from both' → sum all |diff|. Knowing which arithmetic the operation implies is the entire interview signal here.
ComplexityO(|a| + |b|) time, O(1) space — Two linear passes (one per string) and a constant 26-slot scan → O(|a| + |b|) time. One fixed array → O(1) space.
E · Edge cases and pitfalls6a = "abc", b = "cba" — Already anagrams → balance all zero → 0.Explore edge casesHide edge cases
Edge cases handled
a = "abc", b = "cba"Already anagrams → balance all zero → 0.a = "aaa", b = "a"a slot = +2 → 2 deletions from a.a = "", b = ""No characters → 0 deletions.
Pitfalls
- Summing raw balance instead of |balance| — positives and negatives cancel to a wrong total.
- Reusing the Anagram /2 — only valid for single-sided replacement.
- Using ord(ch) without the − 97 offset in Python (index out of range).
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int makingAnagrams(String a, String b) { int[] balance = new int[26]; for (char c : a.toCharArray()) balance[c - 'a']++; for (char c : b.toCharArray()) balance[c - 'a']--; int deletions = 0;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Dividing by two
Deletions are one-sided — removing a surplus from a does not also fix b. Each letter's gap is paid in full, so there is no /2 here (that belongs to the replacement-based Anagram).
Explore the code comparisonHide the code comparison
Dividing by two
Deletions are one-sided — removing a surplus from a does not also fix b. Each letter's gap is paid in full, so there is no /2 here (that belongs to the replacement-based Anagram).
Don't write this
return sum(abs(fa[c] - fb[c]) for c in letters) // 2Write this instead
return sum(abs(fa[c] - fb[c]) for c in letters)2Summing only one map's keys
Letters that appear in b but not a (or vice versa) contribute deletions too. Iterate the union, or all 26 slots.
Explore the code comparisonHide the code comparison
Summing only one map's keys
Letters that appear in b but not a (or vice versa) contribute deletions too. Iterate the union, or all 26 slots.
Don't write this
return sum(abs(fa[c] - fb[c]) for c in fa)Write this instead
return sum(abs(fa[c] - fb[c]) for c in set(fa) | set(fb))3Summing signed balance
Positive (a-surplus) and negative (b-surplus) entries cancel, hiding real deletions. Take the absolute value of each slot before summing.
Explore the code comparisonHide the code comparison
Summing signed balance
Positive (a-surplus) and negative (b-surplus) entries cancel, hiding real deletions. Take the absolute value of each slot before summing.
Don't write this
for (int v : balance) deletions += v;Write this instead
for (int v : balance) deletions += Math.abs(v);Revise
Carry the pattern into the interview
Pattern to remember
Sum of absolute frequency differences
Code pattern to memorize
fa = freq(a), fb = freq(b)
return sum(|fa[c] - fb[c]| for c in 'a'..'z')Rules to remember
- 1Deletions are allowed from both strings, so you sum all absolute differences — no
/2here. - 2A shared letter with equal counts costs nothing; only the mismatch is deleted.
- 3One signed
int[26]works:+1for each char ofa,−1for each char ofb, then sum|entries|. - 4Lengths need not be equal — anagrams just need matching multisets after deletion.
- 526 counters → O(1) space regardless of input size.
Use this pattern when
- Two strings, deletions allowed on either side, make them anagrams
- Cost is per-deletion and you want the minimum total
- Order is irrelevant — only character multiplicities matter
- Lowercase alphabet so a fixed-size array suffices
Choose another pattern when
- Only replacements in one half allowed → Anagram (sum of positive surplus = sum(|diff|)/2)
- Yes/no 'are these anagrams' → just compare frequency maps for equality
- Insertions allowed too → reframe as edit distance, not pure deletion
- Need the resulting common string, not the count → reconstruct from min(fa, fb)
Interview takeaway
When you may delete from both strings, every letter where the two counts disagree forces exactly that many deletions. Summing absolute frequency differences across the alphabet gives the minimum total — and unlike single-sided 'Anagram', there's no division by two because each string pays for its own surplus.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternFrequency-difference counting with two-sided deletion. Reduce both strings to 26-slot histograms and sum the absolute differences. The presence or absence of a /2 is dictated by the operation: two-sided deletion sums everything, single-sided replacement halves it.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- AnagramSingle-sided: split one string and replace in one half — sum positive surplus = sum(|diff|)/2.
- Valid AnagramJust YES/NO — frequencies must match exactly with equal lengths.
- Delete Operation for Two StringsComing soonDeletions to make strings EQUAL (order matters) — that's LCS-based, not frequency-based.
- Minimum Steps to Make Two Strings AnagramComing soonEqual-length, replace in one string only — count positive surplus.