> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Ways to Divide a Long Corridor

> Tested Python solution for LeetCode 2147 with 25 pytest cases. Generate a practice environment with lcpy.

LeetCode 2147, [Hard](/catalog/hard). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/description/).

Generate this problem as a practice environment: tested reference solution, 25 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2147   # by problem number
lcpy gen -s number_of_ways_to_divide_a_long_corridor   # by problem name
```

## Problem

Along a long library corridor, there is a line of seats and decorative plants. You are given 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 number 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.

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

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/12/04/1.png)

```
Input: corridor = "SSPPSPS"
Output: 3
Explanation: 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](https://assets.leetcode.com/uploads/2021/12/04/2.png)

```
Input: corridor = "PPSPSP"
Output: 1
Explanation: 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](https://assets.leetcode.com/uploads/2021/12/12/3.png)

```
Input: corridor = "S"
Output: 0
Explanation: 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'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_ways_to_divide_a_long_corridor/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_ways_to_divide_a_long_corridor/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def number_of_ways(self, corridor: str) -> int:
        mod = 1_000_000_007
        seats = 0
        last_pair_end = -1
        ways = 1
        for i, ch in enumerate(corridor):
            if ch != "S":
                continue
            seats += 1
            if seats % 2 == 0:
                last_pair_end = i
            elif seats > 1:
                # divider positions between the previous pair and this new pair
                ways = ways * (i - last_pair_end) % mod
        if seats == 0 or seats % 2:
            return 0
        return ways
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.