forked from fuwutu/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScramble String.cpp
More file actions
43 lines (36 loc) · 1017 Bytes
/
Scramble String.cpp
File metadata and controls
43 lines (36 loc) · 1017 Bytes
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
class Solution {
public:
bool isScramble(string s1, string s2)
{
if (s1.length() != s2.length())
{
return false;
}
if (s1 == s2)
{
return true;
}
string r1 = s1;
sort(r1.begin(), r1.end());
string r2 = s2;
sort(r2.begin(), r2.end());
if (r1 != r2)
{
return false;
}
for (size_t n = 1; n < s1.length(); ++n)
{
if (isScramble(s1.substr(0, n), s2.substr(0, n))
&& isScramble(s1.substr(n, s1.length() - n), s2.substr(n, s2.length() - n)))
{
return true;
}
if (isScramble(s1.substr(0, n), s2.substr(s2.length() - n, n))
&& isScramble(s1.substr(n, s1.length() - n), s2.substr(0, s2.length() - n)))
{
return true;
}
}
return false;
}
};