MediumHash TableStringCountingUpdated Sep 2026

1347. Minimum Number of Steps to Make Two Strings Anagram

Asked at JPMorgan

Difficulty

Medium

Time

O(n)

Space

O(1)

Pattern

Hash Table

Companies

1

Problem

You get two strings s and t of the same length. In one step you may pick any character in t and change it into a different character.

Return the minimum number of steps needed to turn t into an anagram of s. An anagram means the two strings use exactly the same characters with the same counts — only the order may differ. Since s and t have equal length, matching the character counts is enough.

Think about it as a balance problem. For each letter, compare how many times it appears in s and in t. If t has too few of some letter, you must create it by replacing a letter that t has too many of. Each replacement fixes exactly one missing letter, so the answer is the total number of missing letters.

What one step can fix

s = "bab"bab012s needs two b's and one at = "aba"aba012t has one extra a (red) and is short one bt after 1 stepbba012Replace that a with b and t matches s — 1 step
Each step turns one surplus letter of t into one letter that s needs.

Constraints

  • ●1 <= s.length <= 5 * 10^4
  • ●s.length == t.length
  • ●s and t consist of lowercase English letters only

Examples

Example 1
Inputs = "bab", t = "aba"
Output1
Why`s` needs two 'b' and one 'a'. `t` has two 'a' and one 'b', so it is short one 'b' and has one extra 'a'. Changing that extra 'a' into 'b' gives "bba", which matches `s`. One step is enough.
Example 2
Inputs = "leetcode", t = "practice"
Output5
Why`t` is missing the letters l, e, e, o, d that `s` has, and it carries extra p, r, a, i, c instead. Each of the 5 missing letters needs its own replacement, so 5 steps are required.
Example 3
Inputs = "anagram", t = "mangaar"
Output0
WhyBoth strings contain the same letters with the same counts (a×3, n×1, g×1, r×1, m×1). `t` is already an anagram of `s`, so no steps are needed.

Asked At

1 companies
CompanyDifficulty
JPMorganMediumView all JPMorgan questions →

How to Think About It

1

Characters that t already shares with s (by count) can stay. Only the surplus characters in t need replacing.

2

Key insight: for each letter, if s has more of it than t, that shortfall must be filled by replacing some other character in t. The total shortfall is the answer.

3

Count with one array: add 1 for each char of s, subtract 1 for each char of t. Sum the positive entries.

4

Because the strings have equal length, total surplus equals total shortfall — summing either side gives the same result.

5

Walkthrough: s = "leetcode", t = "practice". s needs l, e, e, o, d style letters that t lacks; the positive differences sum to 5.

Optimal Approach

Step 1: count = [0] * 26.
Step 2: For each i: count[s[i]] += 1, count[t[i]] -= 1.
Step 3: Return the sum of positive counts.

Time: O(n). Space: O(1).

One counter array, sum the positives

count after s = "leetcode", t = "practice"l1e2o1d1p-1r-1a-1i-1c-1Positive slots are letters s needs; negative slots areletters t must give upsum of positives = 1+2+1+1 = 5sum of negatives = -5answer = 5 steps
Add for s, subtract for t, then add up the letters where the count stayed positive.
ComplexityTime O(n)Space O(1)We scan both strings once and keep a fixed 26-slot counter array, so the extra memory does not grow with the input.

What Trips People Up in Real Interviews

1

Summing absolute differences. That counts each replacement twice — once as a surplus and once as a shortage.

2

Trying to match characters position by position. Anagrams ignore order entirely.

3

Sorting both strings and comparing — it works for equality but does not directly count the minimum replacements.

4

Forgetting the equal-length guarantee that makes the surplus and shortage equal.

Solution Code

O(n) time
def minSteps(s, t):
    count = [0] * 26
    for a, b in zip(s, t):
        count[ord(a) - ord('a')] += 1
        count[ord(b) - ord('a')] -= 1
    return sum(c for c in count if c > 0)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Minimum Number of Steps to Make Two Strings Anagram problem?

Minimum Number of Steps to Make Two Strings Anagram gives you two equal-length strings `s` and `t`; in one step you may replace any character of `t`. What is the minimum number of steps to make `t` an anagram of `s`? It reduces to counting how many characters `t` is missing.

How do you solve Minimum Number of Steps to Make Two Strings Anagram?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Minimum Number of Steps to Make Two Strings Anagram?

Minimum Number of Steps to Make Two Strings Anagram is asked at JPMorgan. It is a medium difficulty problem.

What are common mistakes on Minimum Number of Steps to Make Two Strings Anagram?
  • Summing absolute differences. That counts each replacement twice — once as a surplus and once as a shortage.
  • Trying to match characters position by position. Anagrams ignore order entirely.
  • Sorting both strings and comparing — it works for equality but does not directly count the minimum replacements.
  • Forgetting the equal-length guarantee that makes the surplus and shortage equal.