Number of Ways to Divide a Long Corridor LeetCode solution in C++
1class Solution {2public:3int numberOfWays(string corridor) {4// Store 1000000007 in a variable for convenience5const int MOD = 1e9 + 7;67// Initial values of three variables8int zero = 0;9int one = 0;10int two = 1;1112// Compute using derived equations13for (char thing : corridor) {14if (thing == 'S') {15zero = one;16swap(one, two);17} else {18two = (two + zero) % MOD;19}20}2122// Return the result23return zero;24}25};