Skip to content

Commit cb267cf

Browse files
committed
Add Solution.java to problems 1347
1 parent 13f7d73 commit cb267cf

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Minimum Number of Steps to Make Two Strings Anagram
2+
3+
Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character.
4+
5+
Return the minimum number of steps to make t an anagram of s.
6+
7+
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
8+
9+
## Example 1:
10+
```
11+
Input: s = "bab", t = "aba"
12+
Output: 1
13+
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
14+
15+
```
16+
17+
## Example 2:
18+
```
19+
Input: s = "leetcode", t = "practice"
20+
Output: 5
21+
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
22+
23+
```
24+
25+
## Example 3:
26+
```
27+
Input: s = "anagram", t = "mangaar"
28+
Output: 0
29+
Explanation: "anagram" and "mangaar" are anagrams.
30+
31+
```
32+
33+
34+
## Constraints:
35+
* 1 <= s.length <= 50000
36+
* s.length == t.length
37+
* s and t contain lower-case English letters only.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int minSteps(String s, String t) {
5+
Map<Character, Integer> map = new HashMap<>();
6+
for (char c : t.toCharArray()) {
7+
if (map.containsKey(c)) {
8+
map.put(c, map.get(c) + 1);
9+
} else map.put(c, 1);
10+
}
11+
int res = 0;
12+
for (char c : s.toCharArray()) {
13+
if (map.containsKey(c) && map.get(c) > 0) {
14+
map.put(c, map.get(c) - 1);
15+
} else res ++;
16+
}
17+
return res;
18+
}
19+
}

0 commit comments

Comments
 (0)