Skip to content

Commit 69ab5f3

Browse files
committed
2022-05-29 update: added "Rearrange Characters to Make Target String"
1 parent bb57b84 commit 69ab5f3

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,36 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
// https://leetcode.com/problems/rearrange-characters-to-make-target-string/
4+
public class RearrangeCharactersToMakeTargetString {
5+
6+
private final String s;
7+
private final String target;
8+
9+
public RearrangeCharactersToMakeTargetString(String s, String target) {
10+
this.s = s;
11+
this.target = target;
12+
}
13+
14+
public int solution() {
15+
int[] sCount = new int[26];
16+
for (int i = 0; i < s.length(); i++) {
17+
sCount[s.charAt(i) - 97]++;
18+
}
19+
int[] tCount = new int[26];
20+
for (int i = 0; i < target.length(); i++) {
21+
tCount[target.charAt(i) - 97]++;
22+
}
23+
int result = Integer.MAX_VALUE;
24+
for (int i = 0; i < tCount.length; i++) {
25+
if (tCount[i] != 0) {
26+
if (sCount[i] == 0) {
27+
return 0;
28+
} else {
29+
result = Math.min(result, sCount[i] / tCount[i]);
30+
}
31+
}
32+
}
33+
return result;
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.smlnskgmail.jaman.leetcodejava.easy;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class RearrangeCharactersToMakeTargetStringTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertEquals(
12+
2,
13+
new RearrangeCharactersToMakeTargetString(
14+
"ilovecodingonleetcode",
15+
"code"
16+
).solution()
17+
);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)