Skip to content

Commit bacad99

Browse files
authored
Create 36 Scramble string top down.cpp
1 parent 86b1ce8 commit bacad99

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
unordered_map<string, int> ump;
5+
6+
bool Solve(string X, string Y) {
7+
string key = X + " " + Y;
8+
if (ump.find(key) != ump.end()) // if we did not found the and travesed upto end of the map
9+
return ump[key];
10+
11+
if (X.compare(Y) == 0) {
12+
ump[key] = true;
13+
return true;
14+
}
15+
if (X.length() <= 1) {
16+
ump[key] = false;
17+
return false;
18+
}
19+
20+
int n = X.length();
21+
int flag = false;
22+
for (int i = 1; i <= n - 1; i++) {
23+
if ((Solve(X.substr(0, i), Y.substr(n - i, i)) && Solve(X.substr(i), Y.substr(0, n - i))) ||
24+
(Solve(X.substr(0, i), Y.substr(0, i)) && Solve(X.substr(i), Y.substr(i)))) {
25+
flag = true;
26+
break;
27+
}
28+
}
29+
30+
return ump[key] = flag; // store in table for further reference
31+
}
32+
33+
int main() {
34+
string X, Y; cin >> X >> Y;
35+
36+
ump.clear();
37+
38+
if (X.length() != Y.length())
39+
cout << "No\n";
40+
else
41+
Solve(X, Y) ? cout << "Yes\n" : cout << "No\n";
42+
return 0;
43+
}

0 commit comments

Comments
 (0)