本文主要是介绍***Leetcode 87. Scramble String,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参考后, 没做出来
class Solution {
public:bool isScramble(string s1, string s2) {vector<int> v(127);size_t n = s1.size();if (n == 1) return s1[0] == s2[0];for (size_t i = 0; i != n; ++i) {++v[s1[i]];--v[s2[i]];}for (size_t i = 0; i != n; ++i) {if (v[s1[i]] != 0) return false;}for (size_t i = 1; i != n; ++i) {if (isScramble(s1.substr(0, i), s2.substr(0, i)) && isScramble(s1.substr(i, n - i), s2.substr(i, n - i)))return true;if (isScramble(s1.substr(0, i), s2.substr(n - i, i)) && isScramble(s1.substr(i, n - i), s2.substr(0, n - i)))return true;}return false;}
};
class Solution {
public:bool isScramble(string s1, string s2) {size_t n = s1.size();if (n != s2.size()) return false;vector<vector<vector<int> > > flag(n, vector<vector<int> >(n, vector<int>(n + 1)));for (size_t i = 0; i != n; ++i) {for (size_t j = 0; j != n; ++j) {if (s1[i] == s2[j]) flag[i][j][1] = 1;}}for (size_t len = 2; len <= n; ++len) {for (size_t i = 0; i != n; ++i){for (size_t j = 0; j != n; ++j) {if (max(j, i) + len <= n) {for (size_t k = 1; k != len; ++k) {flag[i][j][len] = flag[i][j][len] || (flag[i][j + len - k][k] && flag[i + k][j][len - k]);flag[i][j][len] = flag[i][j][len] || (flag[i][j][k] && flag[i + k][j + k][len - k]);} }}}}return flag[0][0][n];}
};
这篇关于***Leetcode 87. Scramble String的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!