clueless coding // TODO: be smarter

LeetCode Permutation in String: CPP Solution



TL;DR Code Solution



bool checkInclusion(string s1, string s2) {

     unordered_map<char, int> map;
     for (int i = 0; i < s1.length(); i++) {
         map[s1[i]]++;
     }

    int count = s1.length();
    int left = 0;
    int right = 0;
    while (right < s2.length()) {

        map[s2[right]]--;
        if (map[s2[right]] >= 0) {
            count--;
        }
        right++;

        if (count == 0) {
            return true;
        }

        if (right - left == s1.length()) {
            map[s2[left]]++;
            if (map[s2[left]] > 0) {
                count++;
            }
            left++;
        }
    }

    return false;

}