2147. Number of Ways to Divide a Long Corridor LeetCode Solution
In this guide, you will get 2147. Number of Ways to Divide a Long Corridor LeetCode Solution with the best time and space complexity. The solution to Number of Ways to Divide a Long Corridor problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Ways to Divide a Long Corridor solution in C++
- Number of Ways to Divide a Long Corridor solution in Java
- Number of Ways to Divide a Long Corridor solution in Python
- Additional Resources

Problem Statement of Number of Ways to Divide a Long Corridor
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.length
1 <= n <= 105
corridor[i] is either 'S' or 'P'.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2147. Number of Ways to Divide a Long Corridor LeetCode Solution in C++
class Solution {
public:
int numberOfWays(string corridor) {
constexpr int kMod = 1'000'000'007;
long ans = 1;
int prevSeat = -1;
int numSeats = 0;
for (int i = 0; i < corridor.length(); ++i) {
if (corridor[i] == 'S') {
if (++numSeats > 2 && numSeats % 2 == 1)
ans = ans * (i - prevSeat) % kMod;
prevSeat = i;
}
}
return numSeats > 1 && numSeats % 2 == 0 ? ans : 0;
}
};
/* code provided by PROGIEZ */
2147. Number of Ways to Divide a Long Corridor LeetCode Solution in Java
class Solution {
public int numberOfWays(String corridor) {
final int kMod = 1_000_000_007;
long ans = 1;
int prevSeat = -1;
int numSeats = 0;
for (int i = 0; i < corridor.length(); ++i) {
if (corridor.charAt(i) == 'S') {
if (++numSeats > 2 && numSeats % 2 == 1)
ans = ans * (i - prevSeat) % kMod;
prevSeat = i;
}
}
return numSeats > 1 && numSeats % 2 == 0 ? (int) ans : 0;
}
}
// code provided by PROGIEZ
2147. Number of Ways to Divide a Long Corridor LeetCode Solution in Python
class Solution:
def numberOfWays(self, corridor: str) -> int:
kMod = 1_000_000_007
ans = 1
prevSeat = -1
numSeats = 0
for i, c in enumerate(corridor):
if c == 'S':
numSeats += 1
if numSeats > 2 and numSeats % 2 == 1:
ans = ans * (i - prevSeat) % kMod
prevSeat = i
return ans if numSeats > 1 and numSeats % 2 == 0 else 0
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.