Skip to content

Commit

Permalink
solutions: 1347 - Minimum Number of Steps to Make Two Strings Anagram…
Browse files Browse the repository at this point in the history
… (Easy)
  • Loading branch information
wingkwong committed Jan 13, 2024
1 parent 226276c commit 6403490
Showing 1 changed file with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
description: >-
Author: @wingkwong |
Author: @wingkwong, @jaffar |
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/
tags: ['Hash Map']
tags: [Hash Table, String, Counting]
---

# 1347 - Minimum Number of Steps to Make Two Strings Anagram (Easy)
Expand Down Expand Up @@ -49,8 +49,10 @@ Explanation: "anagram" and "mangaar" are anagrams.
* `s.length == t.length`
* `s` and `t` consist of lowercase English letters only.

## Approach 1: Hash Map
## Approach 1: Frequency Count

<Tabs>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@wingkwong"/>

```cpp
Expand All @@ -60,12 +62,27 @@ public:
int ans = 0;
unordered_map<int, int> m;
// Count the frequency of characters of each string.
// Loop over all characters if the frequency of a character in t is less than the frequency of the same character in s
// Loop over all characters if the frequency of a character in t is less than that in s
// then add the difference between the frequencies to the answer.
for (auto x : s) m[x - 'a']++;
for (auto x : t) m[x - 'a']--;
for (auto x : m) if (x.second > 0) ans += x.second;
return ans;
}
};
```
```
</TabItem>
<TabItem value="py" label="Python">
<SolutionAuthor name="@jaffar"/>
```py
class Solution:
def minSteps(self, s: str, t: str) -> int:
return sum((Counter(s) - Counter(t)).values())
```

</TabItem>
</Tabs>

0 comments on commit 6403490

Please sign in to comment.