2251. Number of Ways to Divide a Long Corridor
My accepted Python solution to LeetCode problem 2251, Number of Ways to Divide a Long Corridor, running in 223ms.
- Difficulty: Hard
- Python
- Runtime 223ms
- Memory 21.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 223ms, memory 21.5MB, accepted 2026-01-01.
class Solution:
def numberOfWays(self, corridor: str) -> int:
MOD = 10**9 + 7
# Find positions of all seats
seats = [i for i, c in enumerate(corridor) if c == 'S']
# Need even number of seats >= 2
if len(seats) < 2 or len(seats) % 2 != 0:
return 0
result = 1
# For each pair of seats, count plants between pairs
for i in range(2, len(seats), 2):
# Gap between seat i-1 and seat i
gap = seats[i] - seats[i-1]
result = (result * gap) % MOD
return result