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:
11
💡 Note:
s has: l(1), e(4), t(1), c(1), o(1), d(1). t has: p(1), r(1), a(1), c(2), t(1), i(1), e(1). Character frequency differences: |1-0|+|4-1|+|1-1|+|1-2|+|1-0|+|1-0|+|0-1|+|0-1|+|0-1|+|0-1| = 1+3+0+1+1+1+1+1+1+1 = 11 steps.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code