All problems

Problem 668

Split a String in Balanced Strings

Easy · 10 pts · String, Greedy

Problem

A string of the letters L and R is balanced if it contains the same number of L's and R's. Given a balanced string, split it into the largest possible number of consecutive pieces such that every piece is balanced. Every character must be used. Keep a running balance (+1 for R, -1 for L) and cut the string every time the balance returns to zero; cutting as early as possible gives the most pieces.

Input

A single balanced string S of the letters L and R.

Output

Print the maximum number of balanced pieces.

Constraints

2 <= |S| <= 1000; S is balanced

Example input

RLRRLLRLRL

Example output

4

Explanation

RLRRLLRLRL splits into RL, RRLL, RL and RL, each with equal numbers of L and R. That is 4 pieces, and no split gives more.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Cost to Move Chips to The Same Position Next: Maximum Number of Events That Can Be Attended