Problem · String

Maximum Number of Workers with Equal Left and Right Shoes

EasyTeslaINTERNOA

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 S is made only of the characters 'R' and/or 'L';
  • The number of letters "L" and "R" in string S is the same.
  • Another problem from the same batch:

    Examples
    01 · Example 1
    S = "RLRRLLRLRRLL"
    return = 4
    Because S can be split into intervals: "RL", "RRLL", "RL" and "RRLL". Note that the intervals do not have to be of the same size.
    02 · Example 2
    S = "RLLLRRRLLR"
    return = 4
    The function should return 4, because S can be split into intervals: "RL", "LLRR", "RL" and "LR".
    03 · Example 3
    S = "LLRLRLRLRLRLRR"
    return = 1
    The function should return 1.
    More Tesla problems
    drafts saved locally
    public int teslaShoeFactory(String S) {
      // write your code here
    }
    
    
    S"RLRRLLRLRRLL"
    expected4
    sign in to submit