-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scramble String.cpp
70 lines (66 loc) · 1.43 KB
/
Scramble String.cpp
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
#include<iostream>
#include<string>
#include<vector>
#include <cctype>
#include<algorithm>
#include<math.h>
using namespace std ;
typedef struct ListNode{
int val;
ListNode * next;
} ListNode;
class Solution {
public:
bool isScramble(string s1, string s2) {
int res=1;
check(s1,s2,res);
return res;
}
void check(string s1,string s2,int &res){
// cout<<s1<<" "<<s2<<endl;
int len1=s1.length();
int len2=s2.length();
int i;
if(s1==s2)
return;
if(len1!=len2||(len1==1&&s1!=s2)){
res=0;
return;
}
if(len1==0)
return;
for(i=0;s2[i]!=s1[0];i++);
if(i<len1&&s2[i]==s1[0]){
string sub1=s1.substr(1,i);
string sub2=s2.substr(0,i);
sort(sub1.begin(),sub1.end());
sort(sub2.begin(),sub2.end());
if(sub1==sub2){
check(s1.substr(1,i),s2.substr(0,i),res);
check(s1.substr(i+1,len1-i),s2.substr(i+1,len2-i),res);
}
else{
check(s1.substr(1,i),s2.substr(i+1,len2-i),res);
check(s1.substr(i+1,len1-i),s2.substr(0,i),res);
}
}
else
res=0;
}
void print()
{
//cout<<isScramble("rgtae","great")<<endl;
//cout<<isScramble("rgeat","great")<<endl;
//cout<<isScramble("r","g")<<endl;
cout<<isScramble("abc","cab")<<endl;
//cout<<isScramble("sgeat","sreat")<<endl;
}
};
int main()
{
Solution test;
// cout<<test.Low("A man, a plan, a canal: Panama")<<endl;
test.print();
// cout<<atoi("2147483648")<<endl;
system("pause");
}