Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Leetcode 2147. Number of Ways to Divide a Long Corridor
- class Solution {
- public:
- int numberOfWays(string corridor) {
- string temp;
- long long output = 1;
- int count = 0;
- bool isValid = false;
- for (auto c: corridor) {
- if (c == 'S') {
- count++;
- isValid = true;
- }
- temp.push_back(c);
- if (count == 2) {
- while (count > 0) {
- if (temp.back() == 'S') count--;
- temp.pop_back();
- }
- temp.push_back('X');
- }
- }
- if (count || !isValid) return 0;
- while (temp.back() == 'P') temp.pop_back();
- int i = 0;
- for (; i < temp.size(); i++) {
- if (temp[i] == 'X') break;
- }
- for (; i < temp.size(); i++) {
- // count for P now
- if (temp[i] == 'P') count++;
- else {
- output *= count + 1;
- output %= 1'000'000'007;
- count = 0;
- }
- }
- return output;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment