Split a String in Balanced Strings

A balanced string has the same number of 'L' and 'R' characters. Given a balanced string s, split it into substrings such that every substring is balanced. Return the maximum number of balanced strings you can obtain.

30:00

Split a String in Balanced Strings
easy
Free written solution
Topics
Companies

A balanced string has the same number of 'L' and 'R' characters. Given a balanced string s, split it into substrings such that every substring is balanced. Return the maximum number of balanced strings you can obtain.

Example 1:
Input: {"s":"RLRRLLRLRL"}
Output: 4
Constraints:
  • 2≤s.length≤10002 \leq \text{s.length} \leq 1000

  • s[i]s[i] is either 'L' or 'R'.

  • ss is a balanced string.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"RLRRLLRLRL"}

Read 10 characters left to right. Count R as +1 and L as -1; a piece is balanced when its running total is back at 0.

R

L

R

R

L

L

R

L

R

L

balance after each character (R up, L down); every return to 0 is a cut

0

balance

0

pieces cut

0

Variables
VariableValue
balance0
count0
DepthFunction Call
Stack empty
0/12