> For the complete documentation index, see [llms.txt](https://hao-fu-1.gitbook.io/oj/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hao-fu-1.gitbook.io/oj/array_and_numbers/1347.-minimum-number-of-steps-to-make-two-strings-anagram.md).

# 1347. Minimum Number of Steps to Make Two Strings Anagram

https\://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/

Given two equal-size strings `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.

**Example 1:**

```
Input: s = "bab", t = "aba"
Output: 1
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
```

**Example 2:**

```
Input: s = "leetcode", t = "practice"
Output: 5
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
```

**Example 3:**

```
Input: s = "anagram", t = "mangaar"
Output: 0
Explanation: "anagram" and "mangaar" are anagrams. 
```

**Example 4:**

```
Input: s = "xxyyzz", t = "xxyyzz"
Output: 0
```

**Example 5:**

```
Input: s = "friend", t = "family"
Output: 4
```

**Constraints:**

* `1 <= s.length <= 50000`
* `s.length == t.length`
* `s` and `t` contain lower-case English letters only.

等长的字符串s和t，问让t的字符及频率变得和s一样且不要求顺序一样，至少需要替换t中多少个字符。统计s出现的字符和对应的频率并放到freq map中，遍历t当freq map出现负数时，意味着t中多了s中不存在的字符，又由于t和s等长，所以多了多少字符也就是少了同样数目的其它字符，也就是多了的字符数和要替换的字符数目一致，统计下来即结果。

```cpp
class Solution {
public:
    int minSteps(string s, string t) {
        unordered_map<char, int> freq;
        for (const auto c : s) {
            ++freq[c];
        }
        int res = 0;
        for (const auto c : t) {
            if (freq[c] <= 0) {
                ++res;
            }
            --freq[c];
        }
        return res;
    }
};

```
