Neuenmuller

Leetcode 2147

Nov 27th, 2023
122
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | Source Code | 1 0
  1. // Leetcode 2147. Number of Ways to Divide a Long Corridor
  2. class Solution {
  3. public:
  4.     int numberOfWays(string corridor) {
  5.         string temp;
  6.         long long output = 1;
  7.         int count = 0;
  8.         bool isValid = false;
  9.  
  10.         for (auto c: corridor) {
  11.             if (c == 'S') {
  12.                 count++;
  13.                 isValid = true;
  14.             }
  15.             temp.push_back(c);
  16.  
  17.             if (count == 2) {
  18.                 while (count > 0) {
  19.                     if (temp.back() == 'S') count--;
  20.                     temp.pop_back();
  21.                 }
  22.                 temp.push_back('X');
  23.             }
  24.         }
  25.  
  26.         if (count || !isValid) return 0;
  27.  
  28.         while (temp.back() == 'P') temp.pop_back();
  29.  
  30.         int i = 0;
  31.         for (; i < temp.size(); i++) {
  32.             if (temp[i] == 'X') break;
  33.         }
  34.  
  35.         for (; i < temp.size(); i++) {
  36.             // count for P now
  37.             if (temp[i] == 'P') count++;
  38.             else {
  39.                 output *= count + 1;
  40.                 output %= 1'000'000'007;
  41.                count = 0;
  42.            }
  43.        }
  44.  
  45.        return output;
  46.    }
  47. };
Tags: leetcode
Advertisement
Add Comment
Please, Sign In to add comment