Skip to content

Commit ba869c3

Browse files
add 1347
1 parent 3dfb83c commit ba869c3

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1347|[Minimum Number of Steps to Make Two Strings Anagram](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1347.java) | |Easy|String|
1112
|1346|[Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1346.java) | |Easy|Array|
1213
|1345|[Jump Game IV](https://leetcode.com/problems/jump-game-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1345.java) | |Hard|BFS|
1314
|1344|[Angle Between Hands of a Clock](https://leetcode.com/problems/angle-between-hands-of-a-clock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1344.java) | |Medium|Math|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1347. Minimum Number of Steps to Make Two Strings Anagram
7+
*
8+
* Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character.
9+
* Return the minimum number of steps to make t an anagram of s.
10+
* An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
11+
*
12+
* Example 1:
13+
* Input: s = "bab", t = "aba"
14+
* Output: 1
15+
* Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
16+
*
17+
* Example 2:
18+
* Input: s = "leetcode", t = "practice"
19+
* Output: 5
20+
* Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
21+
*
22+
* Example 3:
23+
* Input: s = "anagram", t = "mangaar"
24+
* Output: 0
25+
* Explanation: "anagram" and "mangaar" are anagrams.
26+
*
27+
* Example 4:
28+
* Input: s = "xxyyzz", t = "xxyyzz"
29+
* Output: 0
30+
*
31+
* Example 5:
32+
* Input: s = "friend", t = "family"
33+
* Output: 4
34+
*
35+
* Constraints:
36+
* 1 <= s.length <= 50000
37+
* s.length == t.length
38+
* s and t contain lower-case English letters only.
39+
* */
40+
public class _1347 {
41+
public static class Solution1 {
42+
public int minSteps(String s, String t) {
43+
int[] counts = new int[26];
44+
for (char c : s.toCharArray()) {
45+
counts[c - 'a']++;
46+
}
47+
for (char c : t.toCharArray()) {
48+
if (counts[c - 'a'] > 0) {
49+
counts[c - 'a']--;
50+
}
51+
}
52+
return Arrays.stream(counts).sum();
53+
}
54+
}
55+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1347;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1347Test {
10+
private static _1347.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1347.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.minSteps("bab", "aba"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(5, solution1.minSteps("leetcode", "practice"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0, solution1.minSteps("anagram", "mangaar"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(0, solution1.minSteps("xxyyzz", "xxyyzz"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(4, solution1.minSteps("friend", "family"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)