fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. unordered_map<char, string> ump;
  4. bool isFeasible(string pattern, string st, int stIndex, int patIndex) {
  5. cout << "1" << endl;
  6. if (stIndex == st.length() || patIndex == pattern.length()) {
  7. //cout << "2" << endl;
  8. if (stIndex == st.length() && patIndex == pattern.length()) {
  9. return true;
  10. }
  11. return false;
  12. }
  13. string patString = "";
  14. for (int i = stIndex; i < st.length(); ) {
  15. cout << "3" << endl;
  16. if (ump.find(pattern[patIndex]) != ump.end()) {
  17. cout << "4" << endl;
  18. string patString = ump[pattern[patIndex]];
  19. for (int j = 0; j < patString.length(); j++) {
  20. cout << "5" << endl;
  21. if (patString[j] != st[i]) {
  22. cout << "6" << endl;
  23. return false;
  24. }
  25. i++;
  26. }
  27. if(isFeasible(pattern, st, i, patIndex + 1)) {
  28. cout << "7" << endl;
  29. return true;
  30. }
  31. }
  32. else {
  33. cout << "8" << endl;
  34. patString += st[i];
  35. cout << "patString is " << patString << " and patIndex value is " << pattern[patIndex] << " index is " << i << " patternIndex is " << patIndex << endl;
  36. ump[pattern[patIndex]] = patString;
  37. if (i == st.length() - 1 && patIndex == pattern.length() - 1 && pattern[patIndex] != st[i]) {
  38. return false;
  39.  
  40. }
  41.  
  42.  
  43. if(isFeasible(pattern, st, i + 1, patIndex + 1)) {
  44. cout << "9" << endl;
  45. return true;
  46. }
  47. ump.erase(pattern[patIndex]);
  48. i++;
  49. }
  50. }
  51. return false;
  52. }
  53.  
  54. int main() {
  55. string pat, str;
  56. cin >> pat >> str;
  57. bool isPossible = isFeasible(pat, str, 0, 0);
  58. cout << isPossible;
  59.  
  60. cout << endl;
  61.  
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0s 15240KB
stdin
ab
aa
stdout
1
3
8
patString is a and patIndex value is a index is 0 patternIndex is 0
1
3
8
patString is a and patIndex value is b index is 1 patternIndex is 1
3
8
patString is aa and patIndex value is a index is 1 patternIndex is 0
1
0