Skip to content

Commit 74d4cab

Browse files
add 1525
1 parent 66beb60 commit 74d4cab

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

+1
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+
|1525|[Number of Good Ways to Split a String](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1525.java) | |Medium|String, Bit Manipulation|
1112
|1524|[Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1524.java) | |Medium|Array, Math|
1213
|1523|[Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1523.java) | |Easy|Math|
1314
|1518|[Water Bottles](https://leetcode.com/problems/water-bottles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1518.java) | |Easy|Greedy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1525 {
4+
public static class Solution1 {
5+
public int numSplits(String s) {
6+
int goodSplits = 0;
7+
int[] left = new int[26];
8+
int[] right = new int[26];
9+
for (int i = 0; i < s.length(); i++) {
10+
char c = s.charAt(i);
11+
right[c - 'a']++;
12+
}
13+
for (int i = 0; i < s.length(); i++) {
14+
char c = s.charAt(i);
15+
left[c - 'a']++;
16+
int distinctCharOnTheLeft = getDistinct(left);
17+
right[c - 'a']--;
18+
int distinctCharOnTheRight = getDistinct(right);
19+
if (distinctCharOnTheLeft == distinctCharOnTheRight) {
20+
goodSplits++;
21+
}
22+
}
23+
return goodSplits;
24+
}
25+
26+
private int getDistinct(int[] count) {
27+
int c = 0;
28+
for (int i : count) {
29+
if (i != 0) {
30+
c++;
31+
}
32+
}
33+
return c;
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1525;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1525Test {
10+
private static _1525.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1525.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.numSplits("aacaba"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1, solution1.numSplits("abcd"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(4, solution1.numSplits("aaaaa"));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)