0% found this document useful (0 votes)
25 views2 pages

Leetcode 567

This document describes a Leetcode problem to check if one string is an anagram of a substring of another string. It defines a Solution class with a checkInclusion method that takes two strings as input. The method uses a hashmap to count the frequency of characters in the first string and slides a window over the second string, decrementing the frequencies and checking for an anagram at each step to return true if any substring is an anagram.

Uploaded by

Dhruv Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Leetcode 567

This document describes a Leetcode problem to check if one string is an anagram of a substring of another string. It defines a Solution class with a checkInclusion method that takes two strings as input. The method uses a hashmap to count the frequency of characters in the first string and slides a window over the second string, decrementing the frequencies and checking for an anagram at each step to return true if any substring is an anagram.

Uploaded by

Dhruv Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Leetcode 567.

Permutation in String
class Solution {
public:

bool isAnagram(unordered_map<char,int> &peek){

for(auto p:peek){
if(p.second!=0)
return false;
}

return true;

bool checkInclusion(string s1, string s2) {

if(s1.size()>s2.size())
return false;

unordered_map<char,int> peek;

for(auto ch:s1)
peek[ch]++;

int i=0;
int k=s1.size();

for(;i<k;i++){
if(peek.count(s2[i]))
peek[s2[i]]--;
}

if(isAnagram(peek))
return true;
for(;i<s2.size();i++){

char leftWindow=s2[i-k];
char enteredWindow=s2[i];

if(peek.count(leftWindow))
peek[leftWindow]++;

if(peek.count(enteredWindow)){
peek[enteredWindow]--;
if(isAnagram(peek))
return true;
}
}

return false;
}
};

You might also like