Medium
Hash TableStringCounting
Updated Sep 2026

Minimum Number of Steps to Make Two Strings Anagram

Asked at JPMorgan

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.

Asked At

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).

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

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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

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.