Number of Ways to Divide a Long Corridor


10 Prerequisite LeetCode Problems
“2147. Number of Ways to Divide a Long Corridor” requires you to calculate the number of ways to divide a space under certain conditions. Here are some simpler problems to prepare for this:
LeetCode 70. Climbing Stairs
 This problem introduces the concept of counting the number of ways to reach a certain state, which is a key aspect of dynamic programming.
LeetCode 198. House Robber
 This problem helps you understand how to construct dynamic programming transitions based on different choices at each step.
LeetCode 221. Maximal Square
 This problem introduces the concept of optimizing over a grid, which is similar to the “Long Corridor” problem.
LeetCode 300. Longest Increasing Subsequence
 This problem can help you understand how to set up and solve dynamic programming problems that require optimizing over all previous states.
LeetCode 322. Coin Change
 This problem is about finding the optimal way to reach a target using a set of choices at each step, which is a key dynamic programming concept.
LeetCode 518. Coin Change 2
 This problem involves counting all possible ways to reach a target, which is similar to the “Long Corridor” problem.
LeetCode 646. Maximum Length of Pair Chain
 This problem helps you understand how to set up dynamic programming transitions in problems where the current state depends on the previous state.
LeetCode 139. Word Break
 This problem involves dividing a string into valid words. The division concept is similar to the “Long Corridor” problem, although the constraints are different.
LeetCode 343. Integer Break
 This problem involves dividing an integer into parts to maximize a product. The division concept is similar to the “Long Corridor” problem.
LeetCode 96. Unique Binary Search Trees
 This problem involves counting the number of structurally unique BST’s, a similar counting problem as in the “Long Corridor” problem.
By practicing these problems, you should gain a solid understanding of how to set up dynamic programming states and transitions, and how to count the number of ways to reach a certain state, which are key to solving the “2147. 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 0indexed 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 nonoverlapping 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.length 1 <= n <= 105 corridor[i] is either ‘S’ or ‘P’.