LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  bool checkInclusion(string s1, string s2) {
    unordered_map<char, int> m1;
    for (auto& x : s1) {
      ++m1[x];
    }
    int l = 0;
    unordered_map<char, int> m2;
    for (int r = 0; r < size(s2); ++r) {
      char c = s2[r];
      ++m2[c];
      while (m2[c] > m1[c]) {
        --m2[s2[l]];
        ++l;
      }
      if (r - l + 1 == size(s1)) {
        return true;
      }
    }
    return false;
  }
};