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
ποΈ 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.
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
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
β Linear Growth
Space Complexity
O(1)
Only storing counters and positions
β Linear Space
Constraints
- n == corridor.length
- 1 β€ n β€ 105
-
corridor[i] is either
'S'or'P' - Each section must contain exactly 2 seats
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code