Number of Ways to Divide a Long Corridor - Problem

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

Input & Output

Example 1 — Basic Case
$ Input: corridor = "SSPPSPS"
Output: 3
💡 Note: 6 seats total (even ✓). Seats at positions [0,1,3,4,6]. Pairs: (0,1), (3,4), (6). Gap between pairs: positions 2-3 gives 2 options, position 5-6 gives 1 option. Total: 2 × 1 = 2. Wait, let me recalculate... Actually 3 ways total.
Example 2 — No Valid Division
$ Input: corridor = "PPSPPP"
Output: 0
💡 Note: Only 1 seat total (odd number), impossible to create sections with exactly 2 seats each.
Example 3 — Minimum Valid Case
$ Input: corridor = "SS"
Output: 1
💡 Note: Exactly 2 seats, only one way to divide: both seats in one section.

Constraints

  • 1 ≤ corridor.length ≤ 105
  • corridor[i] is either 'S' or 'P'

Visualization

Tap to expand
Number of Ways to Divide a Long Corridor INPUT Corridor String: 0 1 2 3 4 5 6 S S P P S P S S = Seat P = Plant Rules: - Each section needs exactly 2 seats - Plants can be anywhere - Dividers between positions Seat Positions: [0, 1, 4, 6] 4 seats total (valid: divisible by 2) ALGORITHM STEPS 1 Find All Seat Positions Seats at: 0, 1, 4, 6 2 Check Validity Count=4, divisible by 2: OK 3 Find Gaps Between Pairs Gap = positions between pairs Pair Analysis: Pair 1: seats[0,1] at pos 0,1 Pair 2: seats[2,3] at pos 4,6 Gap: pos 4 - pos 1 = 3 spots 4 Multiply Gap Choices Divider can go at 2,3,4 Formula: ways = gap_1 * gap_2 * ... ways = 3 (only one gap) FINAL RESULT 3 Valid Division Ways: Way 1: Divider after index 1 SS PPSPS Way 2: Divider after index 2 SSP PSPS Way 3: Divider after index 3 SSPP SPS Output: 3 Each section has exactly 2 seats OK - Valid result! Key Insight: The number of ways equals the PRODUCT of gap sizes between consecutive seat pairs. Between 2nd and 3rd seat (positions 1 and 4), there are 3 positions (2,3,4) where a divider can be placed. Time: O(n) | Space: O(1) - Mathematical pattern recognition approach TutorialsPoint - Number of Ways to Divide a Long Corridor | Mathematical Pattern Recognition Approach
Asked in
Google 25 Facebook 18 Microsoft 15 Amazon 12
28.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen