Number of Ways to Divide a Long Corridor LeetCode solution in Java
1class Solution {2public int numberOfWays(String corridor) {3int numSeats = 0, numPlants = 0;4long dividers = 1;56for (int i = 0; i < corridor.length(); ++i) {7if (corridor.charAt(i) == 'S') numSeats += 1;8if (numSeats == 2 && corridor.charAt(i) == 'P') numPlants += 1;9if (numSeats == 3) {10dividers *= (numPlants + 1);11dividers %= 1000000007;12numSeats = 1;13numPlants = 0;14}15}1617if (numSeats < 2) return 0;18return (int) dividers;19}20}