LeetCode Challenge Day 90 β 2147. Number of Ways to Divide a Long Corridor
Nitin Ahirwal / December 14, 2025
Hey folks π
This is Day 90 of my LeetCode streak π
Today's problem is 2147. Number of Ways to Divide a Long Corridor β a deceptively tricky counting problem that becomes elegant once you spot the pattern.
π Problem Statement
You are given a corridor represented as a string consisting of:
'S'β Seat'P'β Plant
Rules:
- The corridor already has dividers at both ends
- You may add dividers between positions
- Each section must contain exactly two seats
- Plants can be present in any quantity
- Two divisions are different if divider positions differ
Goal:
Return the number of valid ways to divide the corridor modulo 10βΉ + 7.
π‘ Intuition
The key observation is:
Every valid section must contain exactly two seats.
So the problem reduces to grouping seats into pairs.
- If the total number of seats is odd or zero, the answer is immediately
0. - Once seats are paired, the only freedom we have is deciding where to place dividers between consecutive seat-pairs.
- That freedom comes from the number of plants between two seat-pairs.
If there are k plants between the end of one section and the start of the next, we have:
'S'
to place a divider.
The final answer is simply the product of these choices.
π Approach
- Traverse the corridor from left to right.
- Count the number of seats encountered.
- Every time a section of two seats is completed:
- Start counting plants until the next seat appears.
- When the next section begins:
- Multiply the answer by
(plantsBetween + 1) - Reset the plant counter
- Multiply the answer by
- At the end:
- If total seats are odd or zero β return
0 - Otherwise return the accumulated result modulo
10βΉ + 7
- If total seats are odd or zero β return
β±οΈ Complexity Analysis
-
Time Complexity:
O(n)β single pass through the corridor -
Space Complexity:
O(1)β constant extra space
π§βπ» Code (JavaScript)
/**
* @param {string} corridor
* @return {number}
*/
var numberOfWays = function (corridor) {
const MOD = 1_000_000_007;
let seatCount = 0;
let ways = 1;
let plantsBetween = 0;
for (let ch of corridor) {
if (ch === 'S') {
seatCount++;
// first seat of a new section
if (seatCount > 2 && seatCount % 2 === 1) {
ways = (ways * (plantsBetween + 1)) % MOD;
plantsBetween = 0;
}
} else if (seatCount >= 2 && seatCount % 2 === 0) {
// count plants only after completing a section
plantsBetween++;
}
}
// invalid if no seats or odd number of seats
if (seatCount === 0 || seatCount % 2 !== 0) return 0;
return ways;
};
π― Reflection
This problem is a great example of how:
-
Hard problems often hide simple counting logic
-
Focusing on constraints (exactly two seats) removes unnecessary complexity
-
Greedy + math can outperform brute force by a mile
That wraps up Day 90 of my LeetCode challenge π₯
Onwards and upwards π
Happy Coding π¨βπ»