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 s and t. In one step, you can append any character to either s or t.
Return the minimum number of steps to make s and t anagrams of each other.
An anagram of a string is a string that contains the same characters with a different (or the same) ordering.
Example 1:
Input: s = "leetcode", t = "coats" Output: 7 Explanation: - In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcodeas". - In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coatsleede". "leetcodeas" and "coatsleede" are now anagrams of each other. We used a total of 2 + 5 = 7 steps. It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
Example 2:
Input: s = "night", t = "thing" Output: 0 Explanation: The given strings are already anagrams of each other. Thus, we do not need any further steps.
Constraints:
1 <= s.length, t.length <= 2 * 105s and t consist of lowercase English letters.Problem summary: You are given two strings s and t. In one step, you can append any character to either s or t. Return the minimum number of steps to make s and t anagrams of each other. 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
"leetcode" "coats"
"night" "thing"
minimum-number-of-steps-to-make-two-strings-anagram)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2186: Minimum Number of Steps to Make Two Strings Anagram II
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 ans = 0;
for (int v : cnt) {
ans += Math.abs(v);
}
return ans;
}
}
// Accepted solution for LeetCode #2186: Minimum Number of Steps to Make Two Strings Anagram II
func minSteps(s string, t string) int {
cnt := make([]int, 26)
for _, c := range s {
cnt[c-'a']++
}
for _, c := range t {
cnt[c-'a']--
}
ans := 0
for _, v := range cnt {
ans += abs(v)
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2186: Minimum Number of Steps to Make Two Strings Anagram II
class Solution:
def minSteps(self, s: str, t: str) -> int:
cnt = Counter(s)
for c in t:
cnt[c] -= 1
return sum(abs(v) for v in cnt.values())
// Accepted solution for LeetCode #2186: Minimum Number of Steps to Make Two Strings Anagram II
/**
* [2186] Minimum Number of Steps to Make Two Strings Anagram II
*
* You are given two strings s and t. In one step, you can append any character to either s or t.
* Return the minimum number of steps to make s and t anagrams of each other.
* An anagram of a string is a string that contains the same characters with a different (or the same) ordering.
*
* Example 1:
*
* Input: s = "<u>lee</u>tco<u>de</u>", t = "co<u>a</u>t<u>s</u>"
* Output: 7
* Explanation:
* - In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcode<u>as</u>".
* - In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coats<u>leede</u>".
* "leetcodeas" and "coatsleede" are now anagrams of each other.
* We used a total of 2 + 5 = 7 steps.
* It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
*
* Example 2:
*
* Input: s = "night", t = "thing"
* Output: 0
* Explanation: The given strings are already anagrams of each other. Thus, we do not need any further steps.
*
*
* Constraints:
*
* 1 <= s.length, t.length <= 2 * 10^5
* s and t consist of lowercase English letters.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/
// discuss: https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn min_steps(s: String, t: String) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2186_example_1() {
let s = "leetcode".to_string();
let t = "coats".to_string();
let result = 7;
assert_eq!(Solution::min_steps(s, t), result);
}
#[test]
#[ignore]
fn test_2186_example_2() {
let s = "night".to_string();
let t = "thing".to_string();
let result = 0;
assert_eq!(Solution::min_steps(s, t), result);
}
}
// Accepted solution for LeetCode #2186: Minimum Number of Steps to Make Two Strings Anagram II
function minSteps(s: string, t: string): number {
let cnt = new Array(128).fill(0);
for (const c of s) {
++cnt[c.charCodeAt(0)];
}
for (const c of t) {
--cnt[c.charCodeAt(0)];
}
let ans = 0;
for (const v of cnt) {
ans += Math.abs(v);
}
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.