-
Notifications
You must be signed in to change notification settings - Fork 257
/
scramble-string.cpp
53 lines (46 loc) · 1.49 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
// Time: O(n^4)
// Space: O(n^3)
class Solution {
public:
/**
* @param s1 A string
* @param s2 Another string
* @return whether s2 is a scrambled string of s1
*/
bool isScramble(string& s1, string& s2) {
if (s1.length() != s2.length()) {
return false;
}
if (s1 == s2) {
return true;
}
const auto n = s1.length();
// is_cramble[l][i][j] denoted as s1[i:i+l], s2[j:j+l] are scramble.
vector<vector<vector<bool>>>
is_scramble(n + 1, vector<vector<bool>>(n, vector<bool>(n, false)));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s1[i] == s2[j]) {
is_scramble[1][i][j] = true;
}
}
}
// Compute by each length.
for (int l = 2; l <= n; ++l) {
for (int i = 0; i < n - l + 1; ++i) {
for (int j = 0; j < n - l + 1; ++j) {
for (int k = 1; k < l; ++k) {
if ((is_scramble[k][i][j] &&
is_scramble[l - k][i + k][j + k]) ||
(is_scramble[k][i][j + l - k] &&
is_scramble[l - k][i + k][j])) {
is_scramble[l][i][j] = true;
break; // Pruning.
}
}
}
}
}
return is_scramble[n][0][0];
}
};