Number of Ways to Divide a Long Corridor - Problem
Library Corridor Division Challenge

Imagine you're an interior designer tasked with dividing a long library corridor into perfect reading sections! πŸ“š

You're given a string corridor representing a line of seats ('S') and plants ('P'). Your goal is to install room dividers to create sections where each section contains exactly 2 seats and any number of plants.

πŸ—οΈ Setup: Room dividers are already installed at both ends (before index 0 and after index n-1). You can install additional dividers between any two adjacent positions.

🎯 Goal: Count how many different ways you can divide the corridor into valid sections. Two arrangements are different if there's at least one position where one has a divider and the other doesn't.

Return the count modulo 109 + 7, or 0 if no valid division exists.

Input & Output

basic_example.py β€” Python
$ Input: corridor = "SSPPSPS"
β€Ί Output: 3
πŸ’‘ Note: We have 4 seats total, so we need 2 sections with 2 seats each. The sections must be [SS][??] and [??][S]. The plant gaps between sections give us 3 ways to place the divider: after index 2, 3, or 4.
single_section.py β€” Python
$ Input: corridor = "PPSPSP"
β€Ί Output: 1
πŸ’‘ Note: Only 2 seats total, so only one section needed. There's exactly 1 way to arrange this (no internal dividers needed).
impossible_case.py β€” Python
$ Input: corridor = "S"
β€Ί Output: 0
πŸ’‘ Note: Only 1 seat, but each section needs exactly 2 seats. Impossible to create valid sections.

Visualization

Tap to expand
Library Corridor DivisionπŸ’ΊπŸ’ΊReading Nook 1Plant Garden3 wall positionsπŸ’ΊπŸ’ΊReading Nook 2πŸ—οΈ Design Strategy1. Each reading nook needs exactly 2 chairs πŸ’ΊπŸ’Ί2. Plant gardens separate the nooks 🌿🌿🌿3. Each garden offers multiple wall placement optionsTotal designs = Options in Garden 1 Γ— Garden 2 Γ— ...
Understanding the Visualization
1
Count the Chairs
First, count total chairs. If odd number or zero, it's impossible to make pairs.
2
Find Chair Pairs
Identify where each 2-chair reading nook begins and ends.
3
Locate Plant Gardens
Between each pair of nooks, there's a garden area with plants.
4
Count Wall Options
In each plant garden, you can place the dividing wall at multiple positions.
5
Multiply Choices
Total arrangements = ways to place wall in garden 1 Γ— ways in garden 2 Γ— ...
Key Takeaway
🎯 Key Insight: Instead of trying every possible arrangement, count the placement options in each plant gap and multiply them together!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass to find seat positions and count plants

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only storing counters and positions

n
2n
βœ“ Linear Space

Constraints

  • n == corridor.length
  • 1 ≀ n ≀ 105
  • corridor[i] is either 'S' or 'P'
  • Each section must contain exactly 2 seats
Asked in
Google 24 Amazon 18 Meta 12 Microsoft 8
20.7K Views
Medium Frequency
~25 min Avg. Time
876 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