leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2222.cpp (571B)


      1 class Solution {
      2   public:
      3     long long numberOfWays(const string &s) const {
      4         static int count[100001];
      5         const int n = size(s);
      6         int crnt[] = {0, 0};
      7         long long res = 0;
      8 
      9         for (int i = n - 1; i >= 0; i--) {
     10             const int idx = s[i] - '0';
     11             count[i] = crnt[!idx];
     12             crnt[idx]++;
     13         }
     14 
     15         crnt[0] = crnt[1] = 0;
     16         for (int i = 0; i < n; i++) {
     17             const int idx = s[i] - '0';
     18             res += count[i] * crnt[!idx];
     19             crnt[idx]++;
     20         }
     21 
     22         return res;
     23     }
     24 };