[LeetCode] 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int minSteps(string s, string t) {
vector<int> sc(26), tc(26);
int res = 0;
for(auto ch : s) sc[ch-'a']++;
for(auto ch : t) tc[ch-'a']++;
for(int i = 0; i < 26; i++) {
res += abs(sc[i] - tc[i]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/27/PS/LeetCode/minimum-number-of-steps-to-make-two-strings-anagram-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.