Skip to main content
impol.dev

Number of Ways to Divide a Long Corridor

Hard

Along a long library corridor, there is a line of seats and decorative plants. You receive a 0-indexed string corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant.

One room divider has already been installed to the left of index 0 , and another to the right of index n - 1 . Additional room dividers can be installed. For each position between indices i - 1 and i ( 1 <= i <= n - 1 ), at most one divider can be installed.

Divide the corridor into non-overlapping sections, where each section has exactly two seats with any count of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.

Produce the count of ways to divide the corridor . Since the answer may be very large, produce it modulo 10^9 + 7 . If there is no way, produce 0 .

Example 1

Original illustrative diagram 1 for Number of Ways to Divide a Long Corridor

Input: corridor = "SSPPSPS"

Output: 3

Explanation: In this case, there are 3 different ways to divide the corridor.

The black bars in the above image indicate the two room dividers already installed.

Note that in each of the ways, each section has exactly two seats.

Example 2

Original illustrative diagram 2 for Number of Ways to Divide a Long Corridor

Input: corridor = "PPSPSP"

Output: 1

Explanation: In this case, there is only 1 way to divide the corridor, by not installing any additional dividers.

Installing any would create some section that does not have exactly two seats.

Example 3

Original illustrative diagram 3 for Number of Ways to Divide a Long Corridor

Input: corridor = "S"

Output: 0

Explanation: In this case, there is no way to divide the corridor because there will always be a section that does not have exactly two seats.

Constraints

  • n == corridor.length
  • 1 <= n <= 10^5
  • corridor[i] is either 'S' or 'P'.

Did this problem appear in one of your interviews?

Responses stay private.

Acceptance rate: 0%

© 2026 impol.dev. All rights reserved.PrivacyTerms