C++ O(n) time, slide window solution


  • 0
    M
        bool checkInclusion(string s1, string s2) {
            if(s2.size()==0) return false;
            vector<int> gold(26, 0), cur(26, 0);
            int i=0;
            for(i;i<s1.size();i++) {
                gold[s1[i]-'a']++;
                cur[s2[i]-'a']++;
            }
            if(cur==gold) return true;
            for(i;i<s2.size();i++) {
                cur[s2[i-s1.size()]-'a']--;
                cur[s2[i]-'a']++;
                if(cur==gold) return true;
            }
            return false;
        }

  • 0
    A
    This post is deleted!

Log in to reply
 

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