Split a String in Balanced Strings

Easy

Description

Balanced strings are those with equal number of "L" and "R" characters. Given a balanced string s, split it into the maximum number of balanced strings and return that count.

Examples

Input:s = "RLRRLLRLRL"
Output:4
Explanation:

RL, RRLL, RL, RL are balanced.

Input:s = "LRLRLRLR"
Output:4
Explanation:

LR, LR, LR, LR are all balanced. Each pair of adjacent L and R forms a balanced substring, giving us the maximum possible number of splits.

Input:s = "LLLRRRLRRL"
Output:2
Explanation:

LLLRRR, LRRL are balanced. The first 6 characters form one balanced substring (3 L's and 3 R's), and the last 4 characters form another balanced substring (2 L's and 2 R's).

Constraints

  • 2 ≤ s.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!