Skip to content

Instantly share code, notes, and snippets.

@sourabh2k15
Last active January 10, 2018 06:15
Show Gist options
  • Select an option

  • Save sourabh2k15/07809dd4ffa88edc92594fdecc274c84 to your computer and use it in GitHub Desktop.

Select an option

Save sourabh2k15/07809dd4ffa88edc92594fdecc274c84 to your computer and use it in GitHub Desktop.
Leetcode 567. Permutation in String
class Solution {
public:
bool checkInclusion(string s1, string s2) {
unordered_map<char, int> table;
for(char c : s1){
table[c]++;
}
int begin = 0, end = 0, counter = table.size();
while(end < s2.length()){
char endchar = s2[end];
if(table.count(endchar) == 1){
table[endchar]--;
if(table[endchar] == 0) counter--;
}
end++;
while(counter == 0){
if(end - begin == s1.length()) return true;
char startchar = s2[begin];
if(table.count(startchar) == 1){
table[startchar]++;
if(table[startchar] > 0) counter++;
}
begin++;
}
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment