|
| 1 | +<h2><a href="https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor">2251. Number of Ways to Divide a Long Corridor</a></h2><h3>Hard</h3><hr><p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>'S'</code> and <code>'P'</code> where each <code>'S'</code> represents a seat and each <code>'P'</code> represents a plant.</p> |
| 2 | + |
| 3 | +<p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 <= i <= n - 1</code>), at most one divider can be installed.</p> |
| 4 | + |
| 5 | +<p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p> |
| 6 | + |
| 7 | +<p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" /> |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> corridor = "SSPPSPS" |
| 14 | +<strong>Output:</strong> 3 |
| 15 | +<strong>Explanation:</strong> There are 3 different ways to divide the corridor. |
| 16 | +The black bars in the above image indicate the two room dividers already installed. |
| 17 | +Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats. |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" /> |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> corridor = "PPSPSP" |
| 24 | +<strong>Output:</strong> 1 |
| 25 | +<strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers. |
| 26 | +Installing any would create some section that does not have exactly two seats. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong class="example">Example 3:</strong></p> |
| 30 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" /> |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> corridor = "S" |
| 33 | +<strong>Output:</strong> 0 |
| 34 | +<strong>Explanation:</strong> There is no way to divide the corridor because there will always be a section that does not have exactly two seats. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | +<p><strong>Constraints:</strong></p> |
| 39 | + |
| 40 | +<ul> |
| 41 | + <li><code>n == corridor.length</code></li> |
| 42 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 43 | + <li><code>corridor[i]</code> is either <code>'S'</code> or <code>'P'</code>.</li> |
| 44 | +</ul> |
0 commit comments