C++ 6 lines O(n)


  • 2
    V
    bool checkInclusion(string s1, string s2) {
        int s1s = s1.size(), s2s = s2.size(), start = 0, cnt = 0, arr[26] = {};
        for (auto l : s1) ++arr[l - 'a'];
        for (auto i = 0; i < s2s && cnt < s1s; ++i) {
            if (arr[s2[i] - 'a'] > 0) --arr[s2[i] - 'a'], ++cnt;
            else while (s2[start++] != s2[i]) ++arr[s2[start - 1] - 'a'], --cnt;
        }
        return cnt == s1s;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.