LeetCode(87) Scramble String
阿新 • • 發佈:2019-01-10
題目
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
Below is one possible representation of s1 = "great"
:
great / \ gr eat / \ / \ g r e at / \ a t
To scramble the string, we may choose any non-leaf node and swap its two children.
For example, if we choose the node "gr"
and swap its two children, it produces a scrambled string "rgeat"
.
rgeat / \ rg eat / \ / \ r g e at / \ a t
We say that "rgeat"
is a scrambled string of "great"
.
Similarly, if we continue to swap the children of nodes "eat"
"at"
, it produces a scrambled string "rgtae"
.
rgtae / \ rg tae / \ / \ r g ta e / \ t a
We say that "rgtae"
is a scrambled string of "great"
.
分析
看了好幾遍,依然覺得很難的一個題目。參考大牛的解 參考連結。 方法一:遞迴,分為下面情況 字串長度為1:很明顯,兩個字串必須完全相同才可以。字串長度為2:當s1="ab", s2只有"ab"或者"ba"才可以。
對於任意長度的字串,我們可以把字串s1分為a1,b1兩個部分,s2分為a2,b2兩個部分,滿足((a1~a2) && (b1~b2))或者 ((a1~b2) && (a1~b2))
方法二:動態規劃 遞迴解法還是很好理解的,dp這個真考察水平了,反正我還要好好琢磨琢磨。。。 使用了一個三維陣列boolean result[len][len][len],其中第一維為子串的長度,第二維為s1的起始索引,第三維為s2的起始索引。
result[k][i][j]表示s1[i...i+k]是否可以由s2[j...j+k]變化得來。
程式碼
//87. Scramble String
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
/*方法一:遞迴判斷,把兩個字串分別分隔為兩部分*/
bool isScramble1(string s1, string s2) {
if (s1.empty() && s2.empty())
return true;
int l1 = s1.length(), l2 = s2.length();
if (l1 != l2)
return false;
if (l1 == 1)
return s1 == s2;
string tmp1 = s1, tmp2 = s2;
sort(tmp1.begin(), tmp1.end());
sort(tmp2.begin(), tmp2.end());
for (int i = 0; i < l1; ++i)
if (tmp1[i] != tmp2[i])
return false;
bool ret = false;
//以下標i為分割點,將兩個字串分割為兩部分
for (int i = 1; i < l1 && !ret; ++i)
{
string s11 = s1.substr(0, i);
string s12 = s1.substr(i,l1-i);
string s21 = s2.substr(0, i);
string s22 = s2.substr(i,l1-i);
ret = isScramble1(s11, s21) && isScramble1(s12, s22);
if (!ret)
{
s21 = s2.substr(0, l1 - i);
s22 = s2.substr(l1 - i,i);
ret = isScramble1(s11, s22) && isScramble1(s12, s21);
}//if
}//for
return ret;
}
/*方法二:動態規劃*/
bool isScramble(string s1, string s2)
{
int l1 = s1.length(), l2 = s2.length();
if (l1 != l2)
return false;
if (l1 == 0)
return true;
//dp[k][i][j]表示s1[i,i+k]與s2[j,j+k]的關係
vector<vector<vector<int>>> dp(l1, vector<vector<int>>(l1, vector<int>(l1, 0)));
for (int i = 0; i < l1; ++i)
{
for (int j = 0; j < l1; ++j)
{
if (s1[i] == s2[j])
dp[0][i][j] = 1;
}//for
}//for
for (int k = 2; k <= l1; ++k)
{
for (int i = l1 - k; i >= 0; --i)
{
for (int j = l1 - k; j >= 0; --j)
{
bool ret = false;
for (int m = 1; m < k && !ret; ++m)
{
ret = (dp[m - 1][i][j] && dp[k - m - 1][i + m][j + m]) ||
(dp[m - 1][i][j + k - m] && dp[k - m - 1][i + m][j]);
}//for
dp[k - 1][i][j] = ret ? 1 : 0;
}//for
}//for
}//for
return dp[l1 - 1][0][0];
}
};
int main()
{
cout << Solution().isScramble1("great", "rgtae") << endl;
system("pause");
return 0;
}
GitHub連結