Skip to content

Commit dac0180

Browse files
add 1221
1 parent 4b3e0ad commit dac0180

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ _If you like this project, please leave me a star._ ★
3535
|1243|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1243.java) | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs)|Easy||
3636
|1232|[Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1232.java) | [:tv:](https://www.youtube.com/watch?v=_tfiTQNZCbs) |Easy||
3737
|1228|[Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1228.java) | |Easy||
38+
|1221|[Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1221.java) | |Easy|Greedy|
3839
|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java) | |Easy||
3940
|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java) | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy||
4041
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | [:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE)|Easy||
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1221. Split a String in Balanced Strings
5+
*
6+
* Balanced strings are those who have equal quantity of 'L' and 'R' characters.
7+
* Given a balanced string s split it in the maximum amount of balanced strings.
8+
* Return the maximum amount of splitted balanced strings.
9+
*
10+
* Example 1:
11+
* Input: s = "RLRRLLRLRL"
12+
* Output: 4
13+
* Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
14+
*
15+
* Example 2:
16+
* Input: s = "RLLLLRRRLR"
17+
* Output: 3
18+
* Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.
19+
*
20+
* Example 3:
21+
* Input: s = "LLLLRRRR"
22+
* Output: 1
23+
* Explanation: s can be split into "LLLLRRRR".
24+
*
25+
* Example 4:
26+
* Input: s = "RLRRRLLRLL"
27+
* Output: 2
28+
* Explanation: s can be split into "RL", "RRRLLRLL", since each substring contains an equal number of 'L' and 'R'
29+
*
30+
* Constraints:
31+
* 1 <= s.length <= 1000
32+
* s[i] = 'L' or 'R'
33+
* */
34+
public class _1221 {
35+
public static class Solution1 {
36+
public int balancedStringSplit(String s) {
37+
int i = 0;
38+
int balancedCount = 0;
39+
int lCount = 0;
40+
int rCount = 0;
41+
while (i < s.length()) {
42+
if (s.charAt(i) == 'L') {
43+
lCount++;
44+
} else {
45+
rCount++;
46+
}
47+
i++;
48+
if (lCount != 0 && lCount == rCount) {
49+
lCount = 0;
50+
rCount = 0;
51+
balancedCount++;
52+
}
53+
}
54+
return balancedCount;
55+
}
56+
}
57+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1221;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1221Test {
10+
private static _1221.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1221.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.balancedStringSplit("RLRRLLRLRL"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.balancedStringSplit("RLLLLRRRLR"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(1, solution1.balancedStringSplit("LLLLRRRR"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(2, solution1.balancedStringSplit("RLRRRLLRLL"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy