[LeetCode] Minimum Number of Steps to Make Two Strings Anagram

1347. Minimum Number of Steps to Make Two Strings Anagram

You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.

Return the minimum number of steps to make t an anagram of s.

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
class Solution {
public:
int minSteps(string s, string t) {
unordered_map<char, int> freq;
for(auto& ch : s) freq[ch]++;
for(auto& ch : t) freq[ch]--;
int res = 0;
for(auto& [_, v] : freq) res += abs(v);
return res / 2;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/21/PS/LeetCode/minimum-number-of-steps-to-make-two-strings-anagram/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.