Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2186.html
2186. Minimum Number of Steps to Make Two Strings Anagram II (Medium)
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 * 105
s
andt
consist of lowercase English letters.
Similar Questions:
Solution 1. Counting
-
// OJ: https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/ // Time: O(N) // Space: O(1) class Solution { public: int minSteps(string s, string t) { int cnt[26] = {}, ans = 0; for (char c : s) cnt[c - 'a']++; for (char c : t) cnt[c - 'a']--; for (int n : cnt) ans += abs(n); return ans; } };
-
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()) ############ # 2186. Minimum Number of Steps to Make Two Strings Anagram II # https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/ class Solution: def minSteps(self, s: str, t: str) -> int: count1 = Counter(s) count2 = Counter(t) for x in s: if count2[x] > 0: count2[x] -= 1 for x in t: if count1[x] > 0: count1[x] -= 1 return sum(count1.values()) + sum(count2.values())
-
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; } }
-
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 }
-
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; }
-
/** * @param {string} s * @param {string} t * @return {number} */ var minSteps = function (s, t) { let cnt = new Array(26).fill(0); for (const c of s) { ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; } for (const c of t) { --cnt[c.charCodeAt() - 'a'.charCodeAt()]; } let ans = 0; for (const v of cnt) { ans += Math.abs(v); } return ans; };
Discuss
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802576