Loading
Loading
A choreographer writes a routine as a string of steps: L for a step left and R for a step right. A phrase is balanced when it contains the same number of L and R steps. The full routine is balanced.
Cut the routine into consecutive pieces so that every piece is balanced, and return the largest number of pieces possible.
steps = "RLRRLLRLRL"RL | RRLLRLRLbalance returns to 0 after 2 steps — cutRL | RRLL | RLRLbalance hits 0 again after RRLL — cutRL | RRLL | RL | RL4 pieces4Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
RL | RRLL | RL | RL.
Example 2
The balance only returns to zero at the very end.