-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28 Scramble strings.cpp
More file actions
76 lines (58 loc) · 1.42 KB
/
28 Scramble strings.cpp
File metadata and controls
76 lines (58 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Author Kartik Shukla */
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
typedef long long int ll;
#define mod 998244353
#define MOD 1000000007
#define PI 3.14159265358
#define inf 1e9
#define INF 1e18
vector<int> a[200005];
vector<bool> vis(200005,0);
vector<int> dis(200005,0);
vector<int> in(200005,0);
vector<int> low(200005,0);
int timer = 1;
map<string,bool> mp;
bool scramblestrings(string s1,string s2)
{
string key = s1+" "+s2;
if(mp.find(key) != mp.end())
return mp[key];
if(s1.length()!=s2.length())
return mp[key] = false;
int n = s1.length();
if(n == 0)
return mp[key] = true;
if(s1 == s2)
return mp[key] = true;
string s1_copy = s1;
string s2_copy = s2;
sort(s1_copy.begin(),s1_copy.end());
sort(s2_copy.begin(),s2_copy.end());
if(s1_copy!=s2_copy)
return mp[key] = false;
for(int i=1;i<n;i++)
{
if(scramblestrings(s1.substr(0,i),s2.substr(0,i)) && scramblestrings(s1.substr(i,n-i),s2.substr(i,n-i)))
return true;
if(scramblestrings(s1.substr(0,i),s2.substr(n-i,i)) && scramblestrings(s1.substr(i,n-i),s2.substr(0,n-i)))
return true;
}
return mp[key] = false;
}
void solve ()
{
string s1,s2;
cin>>s1;
cin>>s2;
cout<<scramblestrings(s1,s2)<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}