Skip to content

Practice Number of Ways to Divide a Long Corridor

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2147
LeetCode Problem

Number of Ways to Divide a Long Corridor

Problem Statement

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 10^9 + 7. If there is no way, return 0.

Source: Number of Ways to Divide a Long Corridor on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
corridor = "SSPPSPS"
Output
3

# Example 2

Input
corridor = "PPSPSP"
Output
1

# Example 3

Input
corridor = "S"
Output
0

Constraints

n == corridor.length
1 <= n <= 10^5
corridor[i] is either 'S' or 'P'.

Related LeetCode Problems

Practice more problems similar to Number of Ways to Divide a Long Corridor.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.