🎉 One-stop destination for all your technical interview Preparation 🎉
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.
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;
}
};