Number of Ways to Divide a Long Corridor

Hard Topics Company Tags

The string corridor describes a row of seats (S) and plants (P). Its two ends are already closed by dividers. You may add at most one divider in any gap between adjacent characters. Count the ways to partition the entire row so that every section contains exactly two seats and any number of plants. Different sets of divider positions count as different ways. Return the count modulo 1000000007, or 0 if no valid partition exists.

Dashed lines mark possible divider positions between groups of two seats; choose one divider in each such gap.

Example 1:

Number of Ways to Divide a Long Corridor, example 1: illustration of the example

Input: corridor = "SSPSS"

Output: 2

Explanation: The two valid partitions are SS | PSS and SSP | SS. Each section has exactly two seats.

Example 2:

Number of Ways to Divide a Long Corridor, example 2: illustration of the example

Input: corridor = "PSSP"

Output: 1

Explanation: There are exactly two seats in the entire corridor, so the only valid choice is to add no divider.

Example 3:

Number of Ways to Divide a Long Corridor, example 3: illustration of the example

Input: corridor = "SSS"

Output: 0

Explanation: There are three seats. Sections of exactly two seats cannot cover an odd number of seats, so no partition is valid.

Constraints:

  • 1 <= corridor.length <= 100000
  • Every character is S or P.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.