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 109 + 7. If there is no way, return 0.
Example 1:
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:
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:
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.length1 <= n <= 105corridor[i] is either 'S' or 'P'.Problem Overview: You get a corridor represented by a string containing seats (S) and plants (P). The corridor must be divided using partitions so every section contains exactly two seats. The task is to count how many valid ways you can place these dividers.
Approach 1: Counting Seats and Calculating Divisions (O(n) time, O(1) space)
Scan the corridor and group seats in pairs. Each valid section must contain exactly two seats, so the structure becomes fixed once you locate seat positions. After the first pair is completed, count the number of plants between the second seat of the previous pair and the first seat of the next pair. Every plant position between those pairs becomes a potential divider location, so multiply the number of choices for each gap. This produces a running product of valid partition placements. The approach behaves like lightweight dynamic programming because the total number of arrangements accumulates as you process the string.
Approach 2: Two-Pointer Technique (O(n) time, O(1) space)
Use two pointers to locate seat pairs directly. Move the right pointer until two seats are found, marking the end of a section. Then advance the pointer across plants until the next seat appears. The number of plants skipped determines how many divider positions exist between seat pairs. Multiply this count into the answer and repeat until the corridor ends. This pointer-based traversal avoids extra storage and keeps the logic simple while still processing the string in one pass.
Recommended for interviews: The seat-counting multiplication approach is the most common solution. It demonstrates understanding of pattern observation and linear scanning over a string. Interviewers expect the O(n) solution using combinatorial counting from math. A brute-force attempt that tries every partition quickly becomes exponential and is mainly useful to reason about why counting gaps works.
This approach involves counting the total number of 'S' in the string. We need to ensure that the total number of seats is even, as every section must have exactly two seats. If the count of 'S' is not even or less than two, return 0 because no valid sections can be formed.
Initialize a counter for seats and iterate through the corridor. Each time you encounter the second seat in a pair, check how many plants were between this second seat and the previous pair of seats. The count of positions (plants) between these pairs is the spot where dividers can be placed. Compute the total number of ways combine these choices.
This implementation first counts all 'S' in the corridor to determine if it's possible to split the corridor. It then iterates through the string, calculates how many plants ('P') are between every two pairs of seats ('S'), and uses this count to determine the number of ways dividers can be placed.
Python
JavaScript
Time Complexity: O(n), where n is the length of the corridor string because it processes the string in a single pass.
Space Complexity: O(1), since it uses a fixed amount of extra space for variables.
The two-pointer technique helps efficiently track the segments of the corridor. Position both pointers initially at the start of the string. Move the first pointer until two 'S' characters are found, marking the start of a section. Move the second pointer to continue finding the next two 'S' for another section.
Each transition between segments of groups discovered by the pointers represents a potential divider placement, and the number of these potential movements gives the number of possible configurations.
This program approaches the corridor string in one pass, using a variable to determine when to apply the mod operation for calculating potential dividers and calculates the result in a manner similar to counting plants between seat pairs.
Time Complexity: O(n) due to the string being processed a single time.
Space Complexity: O(1), since only a fixed number of state-tracking variables are used.
We design a function dfs(i, k), which represents the number of ways to partition the corridor at the i-th position, having already placed k screens. Then the answer is dfs(0, 0).
The calculation process of the function dfs(i, k) is as follows:
If i geq len(corridor), it means the corridor has been fully traversed. At this point, if k = 2, it indicates that a valid partitioning scheme has been found, so return 1. Otherwise, return 0.
Otherwise, we need to consider the situation at the current position i:
corridor[i] = 'S', it means the current position is a seat, and we increment k by 1.k > 2, it means the number of screens placed at the current position exceeds 2, so return 0.dfs(i + 1, k). If k = 2, we can also choose to place a screen, i.e., dfs(i + 1, 0). We add the results of these two cases and take the result modulo 10^9 + 7, i.e., ans = (ans + dfs(i + 1, k)) bmod mod.Finally, we return dfs(0, 0).
The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the corridor.
We can divide every two seats into a group. Between two adjacent groups of seats, if the distance between the last seat of the previous group and the first seat of the next group is x, then there are x ways to place the screen.
We traverse the corridor, using a variable cnt to record the current number of seats, and a variable last to record the position of the last seat.
When we encounter a seat, we increment cnt by 1. If cnt is greater than 2 and cnt is odd, then we need to place a screen between last and the current seat. The number of ways to do this is ans times (i - last), where ans is the previous number of ways. Then, we update last to the current seat's position i.
Finally, if cnt is greater than 0 and cnt is even, return ans; otherwise, return 0.
The time complexity is O(n), where n is the length of the corridor. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Counting Seats and Calculating Divisions | Time Complexity: O(n), where n is the length of the corridor string because it processes the string in a single pass. Space Complexity: O(1), since it uses a fixed amount of extra space for variables. |
| Two-Pointer Technique | Time Complexity: O(n) due to the string being processed a single time. Space Complexity: O(1), since only a fixed number of state-tracking variables are used. |
| Memoization Search | — |
| Mathematics | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Counting Seats and Calculating Divisions | O(n) | O(1) | Best general solution; simple linear scan and multiplication of valid divider positions |
| Two-Pointer Technique | O(n) | O(1) | Useful when explicitly tracking seat boundaries while scanning the corridor |
Number of Ways to Divide a Long Corridor | Clear Intuition | Dry Run | Leetcode 2147 | MIK • codestorywithMIK • 9,482 views views
Watch 9 more video solutions →Practice Number of Ways to Divide a Long Corridor with our built-in code editor and test cases.
Practice on FleetCode