Ctrl K

Most Even L/R Pieces

Easy
The string moves consists of the letters L and R, with equally many of each. Call a string even if it contains the same number of L and R letters. Cut moves into consecutive non-empty pieces so that every piece is even and the number of pieces is as large as possible. Return that maximum number of pieces.
Example 1
moves='LRRLLR' → 3

Cut it into "LR", "RL", "LR": each piece has one L and one R.

Example 2
moves='LLRRRL' → 2

"LLRR" and "RL". The first piece cannot be shorter: "LL" and "LLR" are not balanced.

Example 3
moves='RRRLLL' → 1
Constraints
  • 2 ≤ len(moves) ≤ 1000
  • moves[i] is 'L' or 'R'
  • moves contains equally many 'L' and 'R'
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEqual Letters Kept Apart HardNo Twin Letters Side by Side MediumSplit Letters Into Palindromes MediumPattern Woven Several Times HardPalindrome From Two-Letter Cards Medium
loading Python…
Input
moves='LRRLLR'
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.