Skip to main content
Back to problems
#2186
Medium Algorithms

Minimum number of steps to make two strings anagram ii

Hash Table String Counting
73.0% acceptance
Feb 25, 2026
606
28
You are given two strings s and t. In one step, you can append any character to s or t. Return the minimum number of steps to make s and t anagrams of each other.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
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().map(|&c| c.abs()).sum()
  }
}