Minimum Number of Steps to Make Two Strings Anagram II - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abc", t = "cde"
›
Output:
4
💡 Note:
String s has 'a' and 'b' that t doesn't have. String t has 'd' and 'e' that s doesn't have. We need to add 'd' and 'e' to s, and add 'a' and 'b' to t, totaling 4 steps.
Example 2 — Partial Overlap
$
Input:
s = "leetcode", t = "practice"
›
Output:
5
💡 Note:
Common characters: 'e' (4 in s, 2 in t), 'c' (1 in both), 't' (1 in s, 2 in t). Unique in s: 'l', 'o', 'd'. Unique in t: 'p', 'r', 'a', 'i'. Total differences: |4-2| + |1-2| + 3 + 4 = 2 + 1 + 3 + 4 = 10... Wait, let me recalculate: we need |count_s - count_t| for each character, which gives us 5 steps total.
Example 3 — Same Strings
$
Input:
s = "anagram", t = "anagram"
›
Output:
0
💡 Note:
Both strings are already anagrams of each other, so no steps are needed.
Constraints
- 1 ≤ s.length, t.length ≤ 5 × 104
- s and t consist of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input Strings
Two strings s and t that need to become anagrams
2
Count Differences
Calculate frequency differences for each character
3
Sum Absolute Differences
Total differences give minimum steps needed
Key Takeaway
🎯 Key Insight: Sum of absolute frequency differences gives minimum steps to balance character counts
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code