Number of Ways to Divide a Long Corridor LeetCode solution in Java

1
class Solution {
2
public int numberOfWays(String corridor) {
3
int numSeats = 0, numPlants = 0;
4
long dividers = 1;
5

6
for (int i = 0; i < corridor.length(); ++i) {
7
if (corridor.charAt(i) == 'S') numSeats += 1;
8
if (numSeats == 2 && corridor.charAt(i) == 'P') numPlants += 1;
9
if (numSeats == 3) {
10
dividers *= (numPlants + 1);
11
dividers %= 1000000007;
12
numSeats = 1;
13
numPlants = 0;
14
}
15
}
16

17
if (numSeats < 2) return 0;
18
return (int) dividers;
19
}
20
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0