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

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.

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.

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.
We must divide the corridor into sections with exactly two seats each. The only places to put dividers are between pairs of seats, and the number of ways is the product of the number of possible divider positions (gaps) between each pair of seat groups.
classSolution {
public:int numberOfWays(string corridor) {
constint mod =1e9+7;
int n = corridor.size(), seats =0;
for (char c : corridor) if (c =='S') ++seats;
if (seats ==0|| seats%2) return0;
longlong ans =1;
int cnt =0, i =0;
while (i < n && corridor[i] !='S') ++i;
for (; i < n; ++i) {
if (corridor[i] =='S') ++cnt;
if (cnt ==2) {
int j = i+1, plants =0;
while (j < n && corridor[j] !='S') { ++plants; ++j; }
if (j < n) ans = ans * (plants+1) % mod;
cnt =0; i = j-1;
}
}
return ans;
}
};
classSolution {
publicintnumberOfWays(String corridor) {
int mod = 1_000_000_007, n = corridor.length(), seats = 0;
for (char c : corridor.toCharArray()) if (c =='S') seats++;
if (seats == 0 || seats%2 != 0) return 0;
long ans = 1;
int cnt = 0, i = 0;
while (i < n && corridor.charAt(i) !='S') i++;
for (; i < n; i++) {
if (corridor.charAt(i) =='S') cnt++;
if (cnt == 2) {
int j = i+1, plants = 0;
while (j < n && corridor.charAt(j) !='S') { plants++; j++; }
if (j < n) ans = ans * (plants+1) % mod;
cnt = 0; i = j-1;
}
}
return (int)ans;
}
}
classSolution {
funnumberOfWays(corridor: String): Int {
val mod = 1_000_000_007
val n = corridor.length
var seats = 0for (c in corridor) if (c =='S') seats++if (seats ==0|| seats%2!=0) return0var ans = 1Lvar cnt = 0; var i = 0while (i < n && corridor[i] !='S') i++while (i < n) {
if (corridor[i] =='S') cnt++if (cnt ==2) {
var j = i+1; var plants = 0while (j < n && corridor[j] !='S') { plants++; j++ }
if (j < n) ans = ans * (plants+1) % mod
cnt = 0; i = j-1 }
i++ }
return ans.toInt()
}
}
classSolution:
defnumberOfWays(self, corridor: str) -> int:
mod =10**9+7 seats = corridor.count('S')
if seats ==0or seats%2:
return0 ans, cnt, i, n =1, 0, 0, len(corridor)
while i < n and corridor[i] !='S':
i +=1while i < n:
if corridor[i] =='S':
cnt +=1if cnt ==2:
j, plants = i+1, 0while j < n and corridor[j] !='S':
plants +=1 j +=1if j < n:
ans = ans * (plants+1) % mod
cnt =0 i = j-1 i +=1return ans
impl Solution {
pubfnnumber_of_ways(corridor: String) -> i32 {
let modv =1_000_000_007;
let n = corridor.len();
let s = corridor.as_bytes();
let seats = s.iter().filter(|&&c| c ==b'S').count();
if seats ==0|| seats%2!=0 { return0; }
letmut ans =1i64;
letmut cnt =0; letmut i =0;
while i < n && s[i] !=b'S' { i +=1; }
while i < n {
if s[i] ==b'S' { cnt +=1; }
if cnt ==2 {
letmut j = i+1; letmut plants =0;
while j < n && s[j] !=b'S' { plants +=1; j +=1; }
if j < n { ans = ans * (plants+1) % modv; }
cnt =0; i = j-1;
}
i +=1;
}
ans asi32 }
}