-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path87.cpp
More file actions
23 lines (23 loc) · 705 Bytes
/
87.cpp
File metadata and controls
23 lines (23 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool isScramble(string s1, string s2) {
if(s1==s2)
return true;
vector<int> alpha(26,0);
int n = s1.size();
for(int i=0; i<n; i++){
alpha[s1[i]-'a']++;
alpha[s2[i]-'a']--;
}
for(int i=0;i<26;i++)
if(alpha[i]!=0)
return false;
for(int i=1;i<n;i++){
if(isScramble(s1.substr(0,i) , s2.substr(0,i)) && isScramble(s1.substr(i) , s2.substr(i)))
return true;
if(isScramble(s1.substr(0,i) , s2.substr(n-i)) && isScramble(s1.substr(i) , s2.substr(0,n-i)))
return true;
}
return false;
}
};