Minimum Number of Chairs in a Waiting Room

You are given a string s. Simulate events at each second i:

  • If s[i] == 'E', a person enters the waiting room and takes one of the chairs in it.
  • If s[i] == 'L', a person leaves the waiting room, freeing up a chair.

Return the minimum number of chairs needed so that a chair is available for every person who enters the waiting room, given that it is initially empty.

Example 1
Inputs = "EEEEEEE"
Output7
After each second, a person enters the waiting room and no person leaves it, so a minimum of 7 chairs is needed.
Example 2
Inputs = "ELELEEL"
Output2
With 2 chairs, every entering person has a chair available throughout the sequence.

Constraints

  • 1 <= s.length <= 50
  • s consists only of the letters 'E' and 'L'.
  • s represents a valid sequence of entries and exits.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate