Maximum Number of Workers with Equal Left and Right Shoes
Problem statement
Ellen would like to assign this task to her subordinate workers. Each worker should get a distinct interval of adjacent shoes, such that the number of left shoes is equal to the number of right shoes. Each shoe must be assigned to exactly one worker.
What is the maximum number of workers that Ellen can assign to this task?
Write a function:
class Solution { public int solution(String S); }
that, given a string S of letters "L" and "R", denoting the types of shoes in line (left or right), returns the maximum number of intervals such that each interval contains an equal number of left and right shoes.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- String
Sis made only of the characters 'R' and/or 'L'; - The number of letters "L" and "R" in string
Sis the same.
Another problem from the same batch:
Function
teslaShoeFactory(S: String) → intExamples
Example 1
S = "RLRRLLRLRRLL"return = 4Because S can be split into intervals: "RL", "RRLL", "RL" and "RRLL". Note that the intervals do not have to be of the same size.
Example 2
S = "RLLLRRRLLR"return = 4The function should return 4, because S can be split into intervals: "RL", "LLRR", "RL" and "LR".
Example 3
S = "LLRLRLRLRLRLRR"return = 1The function should return 1.