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
Constraints
- ●
1 <= s.length <= 5 * 10^4 - ●
s.length == t.length - ●
s and t consist of lowercase English letters only
Examples
s = "bab", t = "aba"1s = "leetcode", t = "practice"5s = "anagram", t = "mangaar"0Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| JPMorgan | Medium | View all JPMorgan questions → |
How to Think About It
Characters that t already shares with s (by count) can stay. Only the surplus characters in t need replacing.
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.
Count with one array: add 1 for each char of s, subtract 1 for each char of t. Sum the positive entries.
Because the strings have equal length, total surplus equals total shortfall — summing either side gives the same result.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n) timedef 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)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.