Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
"SSPPSPS"
"PPSPSP"
"S"
decode-ways-ii)minimum-cost-to-cut-a-stick)ways-to-split-array-into-three-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2147: Number of Ways to Divide a Long Corridor
class Solution {
private int n;
private char[] s;
private Integer[][] f;
private final int mod = (int) 1e9 + 7;
public int numberOfWays(String corridor) {
s = corridor.toCharArray();
n = s.length;
f = new Integer[n][3];
return dfs(0, 0);
}
private int dfs(int i, int k) {
if (i >= n) {
return k == 2 ? 1 : 0;
}
if (f[i][k] != null) {
return f[i][k];
}
k += s[i] == 'S' ? 1 : 0;
if (k > 2) {
return 0;
}
int ans = dfs(i + 1, k);
if (k == 2) {
ans = (ans + dfs(i + 1, 0)) % mod;
}
return f[i][k] = ans;
}
}
// Accepted solution for LeetCode #2147: Number of Ways to Divide a Long Corridor
func numberOfWays(corridor string) int {
n := len(corridor)
f := make([][3]int, n)
for i := range f {
f[i] = [3]int{-1, -1, -1}
}
const mod = 1e9 + 7
var dfs func(int, int) int
dfs = func(i, k int) int {
if i >= n {
if k == 2 {
return 1
}
return 0
}
if f[i][k] != -1 {
return f[i][k]
}
if corridor[i] == 'S' {
k++
}
if k > 2 {
return 0
}
f[i][k] = dfs(i+1, k)
if k == 2 {
f[i][k] = (f[i][k] + dfs(i+1, 0)) % mod
}
return f[i][k]
}
return dfs(0, 0)
}
# Accepted solution for LeetCode #2147: Number of Ways to Divide a Long Corridor
class Solution:
def numberOfWays(self, corridor: str) -> int:
@cache
def dfs(i: int, k: int) -> int:
if i >= len(corridor):
return int(k == 2)
k += int(corridor[i] == "S")
if k > 2:
return 0
ans = dfs(i + 1, k)
if k == 2:
ans = (ans + dfs(i + 1, 0)) % mod
return ans
mod = 10**9 + 7
ans = dfs(0, 0)
dfs.cache_clear()
return ans
// Accepted solution for LeetCode #2147: Number of Ways to Divide a Long Corridor
impl Solution {
pub fn number_of_ways(corridor: String) -> i32 {
let n: usize = corridor.len();
let bytes = corridor.as_bytes();
let modv: i32 = 1_000_000_007;
let mut f = vec![vec![-1; 3]; n];
fn dfs(
i: usize,
k: usize,
n: usize,
bytes: &[u8],
f: &mut Vec<Vec<i32>>,
modv: i32,
) -> i32 {
if i >= n {
return if k == 2 { 1 } else { 0 };
}
if f[i][k] != -1 {
return f[i][k];
}
let mut nk = k;
if bytes[i] == b'S' {
nk += 1;
}
if nk > 2 {
return 0;
}
let mut res = dfs(i + 1, nk, n, bytes, f, modv);
if nk == 2 {
res = (res + dfs(i + 1, 0, n, bytes, f, modv)) % modv;
}
f[i][k] = res;
res
}
dfs(0, 0, n, bytes, &mut f, modv)
}
}
// Accepted solution for LeetCode #2147: Number of Ways to Divide a Long Corridor
function numberOfWays(corridor: string): number {
const n = corridor.length;
const mod = 10 ** 9 + 7;
const f: number[][] = Array.from({ length: n }, () => Array(3).fill(-1));
const dfs = (i: number, k: number): number => {
if (i >= n) {
return k === 2 ? 1 : 0;
}
if (f[i][k] !== -1) {
return f[i][k];
}
if (corridor[i] === 'S') {
++k;
}
if (k > 2) {
return (f[i][k] = 0);
}
f[i][k] = dfs(i + 1, k);
if (k === 2) {
f[i][k] = (f[i][k] + dfs(i + 1, 0)) % mod;
}
return f[i][k];
};
return dfs(0, 0);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.