119. Even Splits

A string s is made only of the letters L and R, and it contains the same number of each. A pattern is even if it has as many L as R letters. You want to cut s into the largest possible number of consecutive pieces so that every piece is even.

Return that largest number of pieces. The pieces must cover the whole string without overlaps, and the string itself is always a valid single piece. Solve it with one pass and a running balance.

Example 1

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

Cut after the fourth letter: LLRR and LR are both even, giving two pieces.

Example 2

Input:
s = "LRLRLR"
Output:
3
Explanation:

Every pair LR is even on its own, so the string splits into three pieces.

Constraints

2 ≤ s.length ≤ 105

s contains only L and R

s has equally many L and R characters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…