fork download
  1. #include<iostream>
  2. #include<map>
  3. using namespace std;
  4.  
  5. int solve(string A, string B) {
  6. map<char, int> hashMapA, hashMapB;
  7. for(char c : A) hashMapA[c]++;
  8. int start = 0;
  9. for(int end = 0; end < B.length(); end++) {
  10. char c = B[end];
  11. hashMapB[c]++;
  12. while(hashMapB[c] > hashMapA[c] && start <= end) {
  13. hashMapB[ B[start] ]--;
  14. start++;
  15. }
  16. if(end - start + 1 == A.length())
  17. return true;
  18. }
  19.  
  20. return false;
  21. }
  22.  
  23. int main() {
  24. cout << solve("abcd", "hbadcg") << '\n';
  25. cout << solve("aabc", "abcag") << '\n';
  26. cout << solve("aabcd", "adcbga") << '\n';
  27. cout << solve("abcd", "abbcdg") << '\n';
  28. cout << solve("abc", "cbeccbba") << '\n';
  29. cout << solve("abc", "cebbaabc") << '\n';
  30. }
  31.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1
1
0
0
0
1