LeetCode 87. 扰乱字符串
原题链接
困难
作者:
回归线
,
2021-04-19 21:15:12
,
所有人可见
,
阅读 258
class Solution {
public:
bool isScramble(string s1, string s2) {
int n = s1.size();
vector<vector<vector<bool>>> f(n, vector<vector<bool>>(n, vector<bool>(n + 1)));
for (int len = 1; len <= n; ++len) {
for (int i = 0; i + len - 1 < n; ++i ) {
for (int j = 0; j + len - 1 < n; ++j) {
if (len == 1) {
if (s1[i] == s2[j]) {
f[i][j][len] = true;
}
} else {
for (int k = 1; k < len; ++k) {
if ((f[i][j][k] && f[i + k][j + k][len - k]) || (f[i][j + len - k][k] && f[i + k][j][len - k])) {
f[i][j][len] = true;
break;
}
}
}
}
}
}
return f[0][0][n];
}
};