Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

567. Permutation in String 🌟🌟

Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1’s permutations is the substring of s2.

O(N) Time and O(1) Space

Code

class Solution{
public:
    bool checkInclusion(string s1, string s2){
        int m = s1.size(), n = s2.size();
        if (m > n) return false;
        vector<int> cnt1(26, 0), cnt2(26, 0);
        for (int i = 0; i < m; i++){
            cnt1[s1[i] - 'a']++;
            cnt2[s2[i] - 'a']++;
        }
        if (cnt1 == cnt2) return true;

        for (int i = m; i < n; i++){
            cnt2[s2[i] - 'a']++;
            cnt2[s2[i - m] - 'a']--;
            if (cnt1 == cnt2) return true;
        }
        return false;
    }
};