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)


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