#1347
Medium Algorithms Minimum number of steps to make two strings anagram
Hash Table String Counting
82.5% acceptance
Feb 25, 2026
2820
121
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn min_steps(s: String, t: String) -> i32 {
let mut cnt = [0i32; 26];
for b in s.bytes() { cnt[(b - b'a') as usize] += 1; }
for b in t.bytes() { cnt[(b - b'a') as usize] -= 1; }
cnt.iter().filter(|&&x| x > 0).sum()
}
}