Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
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.
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 * 104s.length == t.lengths and t consist of lowercase English letters only.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"bab" "aba"
"leetcode" "practice"
"anagram" "mangaar"
determine-if-two-strings-are-close)minimum-number-of-steps-to-make-two-strings-anagram-ii)minimum-operations-to-make-character-frequencies-equal)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1347: Minimum Number of Steps to Make Two Strings Anagram
class Solution {
public int minSteps(String s, String t) {
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
cnt[c - 'a']++;
}
int ans = 0;
for (char c : t.toCharArray()) {
if (--cnt[c - 'a'] < 0) {
ans++;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1347: Minimum Number of Steps to Make Two Strings Anagram
func minSteps(s string, t string) (ans int) {
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
for _, c := range t {
cnt[c-'a']--
if cnt[c-'a'] < 0 {
ans++
}
}
return
}
# Accepted solution for LeetCode #1347: Minimum Number of Steps to Make Two Strings Anagram
class Solution:
def minSteps(self, s: str, t: str) -> int:
cnt = Counter(s)
ans = 0
for c in t:
cnt[c] -= 1
ans += cnt[c] < 0
return ans
// Accepted solution for LeetCode #1347: Minimum Number of Steps to Make Two Strings Anagram
struct Solution;
impl Solution {
fn min_steps(s: String, t: String) -> i32 {
let mut count: Vec<i32> = vec![0; 26];
for c in s.chars() {
let i = (c as u32 - 'a' as u32) as usize;
count[i] += 1;
}
for c in t.chars() {
let i = (c as u32 - 'a' as u32) as usize;
count[i] -= 1;
}
count.iter().map(|x| x.abs()).sum::<i32>() / 2
}
}
#[test]
fn test() {
let s = "bab".to_string();
let t = "aba".to_string();
let res = 1;
assert_eq!(Solution::min_steps(s, t), res);
let s = "leetcode".to_string();
let t = "practice".to_string();
let res = 5;
assert_eq!(Solution::min_steps(s, t), res);
let s = "anagram".to_string();
let t = "mangaar".to_string();
let res = 0;
assert_eq!(Solution::min_steps(s, t), res);
let s = "xxyyzz".to_string();
let t = "xxyyzz".to_string();
let res = 0;
assert_eq!(Solution::min_steps(s, t), res);
let s = "friend".to_string();
let t = "family".to_string();
let res = 4;
assert_eq!(Solution::min_steps(s, t), res);
}
// Accepted solution for LeetCode #1347: Minimum Number of Steps to Make Two Strings Anagram
function minSteps(s: string, t: string): number {
const cnt: number[] = Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt(0) - 97];
}
let ans = 0;
for (const c of t) {
if (--cnt[c.charCodeAt(0) - 97] < 0) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.