Skip to content

Commit b76d354

Browse files
committed
Split a String in Balanced Strings
1 parent fdd13d5 commit b76d354

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/split-a-string-in-balanced-strings/
3+
4+
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
5+
Given a balanced string s split it in the maximum amount of balanced strings.
6+
Return the maximum amount of splitted balanced strings.
7+
8+
Example 1:
9+
Input: s = "RLRRLLRLRL"
10+
Output: 4
11+
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains
12+
same number of 'L' and 'R'.
13+
14+
Example 2:
15+
Input: s = "RLLLLRRRLR"
16+
Output: 3
17+
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same
18+
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
29+
an equal number of 'L' and 'R'
30+
31+
Constraints:
32+
1 <= s.length <= 1000
33+
s[i] = 'L' or 'R'
34+
"""
35+
class Solution:
36+
def balancedStringSplit(self, s: str) -> int:
37+
count = res = 0
38+
for c in s:
39+
count += 1 if c == 'L' else -1
40+
res += count == 0
41+
return res

0 commit comments

Comments
 (0)