Minimum Number of Steps to Make Two Strings Anagram
MediumUpdated: Aug 2, 2025
Practice on:
Problem
You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.
Return the minimum number of steps to make t an anagram of s.
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
Examples
Example 1
Input: s = "bab", t = "aba"
Output: 1
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
Example 2
Input: s = "leetcode", t = "practice"
Output: 5
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
Example 3
Input: s = "anagram", t = "mangaar"
Output: 0
Explanation: "anagram" and "mangaar" are anagrams.
Constraints
1 <= s.length <= 5 * 10^4s.length == t.lengthsandtconsist of lowercase English letters only.
Solution
Method 1 – Counting Character Differences
Intuition
To make t an anagram of s, we need to match the character counts. For each character, count how many more are needed in t to match s. The sum of positive differences gives the minimum steps.
Approach
- Count frequency of each character in s and t.
- For each character, if t has fewer than s, add the difference to the answer.
- The total is the minimum steps needed.
Code
C++
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int minSteps(string s, string t) {
vector<int> cnt(26, 0);
for (char c : s) cnt[c - 'a']++;
for (char c : t) cnt[c - 'a']--;
int res = 0;
for (int x : cnt) if (x > 0) res += x;
return res;
}
};
Go
func minSteps(s, t string) int {
cnt := make([]int, 26)
for _, c := range s {
cnt[c-'a']++
}
for _, c := range t {
cnt[c-'a']--
}
res := 0
for _, x := range cnt {
if x > 0 {
res += x
}
}
return res
}
Java
class Solution {
public int minSteps(String s, String t) {
int[] cnt = new int[26];
for (char c : s.toCharArray()) cnt[c - 'a']++;
for (char c : t.toCharArray()) cnt[c - 'a']--;
int res = 0;
for (int x : cnt) if (x > 0) res += x;
return res;
}
}
Kotlin
class Solution {
fun minSteps(s: String, t: String): Int {
val cnt = IntArray(26)
for (c in s) cnt[c - 'a']++
for (c in t) cnt[c - 'a']--
var res = 0
for (x in cnt) if (x > 0) res += x
return res
}
}
Python
class Solution:
def minSteps(self, s: str, t: str) -> int:
from collections import Counter
cs, ct = Counter(s), Counter(t)
return sum(max(cs[c] - ct[c], 0) for c in cs)
Rust
use std::collections::HashMap;
impl Solution {
pub fn min_steps(s: String, t: String) -> i32 {
let mut cs = [0; 26];
let mut ct = [0; 26];
for c in s.chars() { cs[(c as u8 - b'a') as usize] += 1; }
for c in t.chars() { ct[(c as u8 - b'a') as usize] += 1; }
(0..26).map(|i| (cs[i] - ct[i]).max(0)).sum()
}
}
TypeScript
class Solution {
minSteps(s: string, t: string): number {
const cs = Array(26).fill(0), ct = Array(26).fill(0);
for (const c of s) cs[c.charCodeAt(0) - 97]++;
for (const c of t) ct[c.charCodeAt(0) - 97]--;
let res = 0;
for (let i = 0; i < 26; ++i) if (cs[i] + ct[i] > 0) res += cs[i] + ct[i];
return res;
}
}
Complexity
- ⏰ Time complexity:
O(n)— n = length of s (and t), single pass for counts. - 🧺 Space complexity:
O(1)— constant extra space.