Guest User

Untitled

a guest
Jan 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool checkInclusion(string s1, string s2) {
  4. unordered_map<char, int> table;
  5.  
  6. for(char c : s1){
  7. table[c]++;
  8. }
  9.  
  10. int begin = 0, end = 0, counter = table.size();
  11.  
  12. while(end < s2.length()){
  13. char endchar = s2[end];
  14.  
  15. if(table.count(endchar) == 1){
  16. table[endchar]--;
  17. if(table[endchar] == 0) counter--;
  18. }
  19.  
  20. end++;
  21.  
  22. while(counter == 0){
  23. if(end - begin == s1.length()) return true;
  24.  
  25. char startchar = s2[begin];
  26.  
  27. if(table.count(startchar) == 1){
  28. table[startchar]++;
  29. if(table[startchar] > 0) counter++;
  30. }
  31.  
  32. begin++;
  33. }
  34. }
  35.  
  36. return false;
  37. }
  38. };
Add Comment
Please, Sign In to add comment