-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScramble String.txt
31 lines (30 loc) · 1.12 KB
/
Scramble String.txt
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
class Solution {
public:
bool isScramble(string s1, string s2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (s1.size() != s2.size())
return false;
int size = s1.size(), plan[size][size][size];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
plan[0][i][j] = (s1[i] == s2[j])?true:false;
for (int l = 1; l < size; l++) {
int len = l + 1;
for (int i = 0; i < size - l; i++)
for (int j = 0; j < size - l; j++)
for (int k = 1; k < len; k++) {
plan[l][i][j] = false;
if (plan[k-1][i][j] && plan[len-k-1][i+k][j+k]) {
plan[l][i][j] = true;
break;
}
if (plan[k-1][i][j+len-k] && plan[len-k-1][i+k][j]) {
plan[l][i][j] = true;
break;
}
}
}
return plan[size-1][0][0];
}
};