10. Permutation in String

3rd Solution is 93% faster than all submissions

Generate all permutations and see if it is present

bool res = false;
bool checkInclusion(string s1, string s2) {
    permute(s1, s2, 0);
    return res;
}
void permute(string s1, string s2, int i) {
    if(i >= s1.length()) {
        if(s2.find(s1) != string::npos)
            res = true;
    } else {
        for(int j = i; j < s1.length(); j++) {
            swap(s1[i], s1[j]);
            permute(s1, s2, i + 1);
            swap(s1[i], s1[j]);
        }
    }

}

Time Complexity: $$O(n!)$$ TLE

Count number of chars in each window using map

Time Complexity: $$O(n^2)$$ TLE

Count number of chars in each window using lookup table

Lesson: Map is slower than lookup table

Time Complexity: Accepted

O(n)O(n)

Space Complexity:

O(n)O(n)

Last updated