Last active
January 10, 2018 06:15
-
-
Save sourabh2k15/07809dd4ffa88edc92594fdecc274c84 to your computer and use it in GitHub Desktop.
Leetcode 567. Permutation in String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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