leetcode

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

1461.cpp (650B)


      1 class Solution {
      2   public:
      3     bool hasAllCodes(const string &s, const int k) const {
      4         static int seen[1 << 20];
      5 
      6         if (size(s) < (1 << k) + k - 1) return false;
      7 
      8         int crnt = 0;
      9         for (int i = 0; i < k; i++) {
     10             crnt = (crnt << 1) | (s[i] & 1);
     11         }
     12 
     13         const int mask = (1 << k) - 1;
     14         int res = 0;
     15 
     16         memset(seen, 0x00, sizeof(seen));
     17         for (int i = k; i < size(s); i++) {
     18             if (!seen[crnt]) seen[crnt] = true, res++;
     19             crnt = ((crnt << 1) | (s[i] & 1)) & mask;
     20         }
     21         if (!seen[crnt]) seen[crnt] = true, res++;
     22         return res == (1 << k);
     23     }
     24 };