[Leetcode] 87. Scramble String 解题报告
2017-03-31 16:45
801 查看
题目:
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 =
To scramble the string, we may choose any non-leaf node and swap its two children.
For example, if we choose the node
string
We say that
Similarly, if we continue to swap the
4000
children of nodes
it produces a scrambled string
We say that
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
思路:
1、递归法:如果s1是s2的scramble string,那么要么s1和s2相等,要么在从s1变换到s2的过程中的最后一步,必然是从s1的中间某个部分切开,形成s11和s12,并且s11和s12都分别可以scramble到s2的前(后)部分或者后(前)部分。所以我们可以递归地求解这一问题。为了加快速度,可以进行适当剪枝:1)两者长度不等;2)两者所含有的可重复字母集合不同。二递归基则是字符串长度为1的情况。
2、动态规划法:定义dp[i][j][k]表示s1.substr(i, k)和s2.substr(j, k)是否是scramble string,则递推方程很容易写出来(具体递推方程可见代码实现)。该动态规划方法的时间复杂度是O(n^4),空间复杂度是O(n^3)。感觉也不是很好,不过所有测试用例倒是都可以通过。
代码:
1、递归法:
2、动态规划法:
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
4000
children of nodes
"eat"and
"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".
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
思路:
1、递归法:如果s1是s2的scramble string,那么要么s1和s2相等,要么在从s1变换到s2的过程中的最后一步,必然是从s1的中间某个部分切开,形成s11和s12,并且s11和s12都分别可以scramble到s2的前(后)部分或者后(前)部分。所以我们可以递归地求解这一问题。为了加快速度,可以进行适当剪枝:1)两者长度不等;2)两者所含有的可重复字母集合不同。二递归基则是字符串长度为1的情况。
2、动态规划法:定义dp[i][j][k]表示s1.substr(i, k)和s2.substr(j, k)是否是scramble string,则递推方程很容易写出来(具体递推方程可见代码实现)。该动态规划方法的时间复杂度是O(n^4),空间复杂度是O(n^3)。感觉也不是很好,不过所有测试用例倒是都可以通过。
代码:
1、递归法:
class Solution { public: bool isScramble(string s1, string s2) { if (s1.length() != s2.length()) { return false; } if (s1 == s2) { return true; } if (s1.length() == 1) { return false; } unordered_map<char, int> hash; int len1 = s1.length(), len2 = s2.length(); for (int i = 0; i < len1; ++i) { hash[s1[i]]++; } for (int i = 0; i < len2; ++i) { hash[s2[i]]--; } for (auto it = hash.begin(); it != hash.end(); ++it) { if (it->second != 0) { return false; } } for (int i = 1; i < len1; ++i) { bool ret = isScramble(s1.substr(0, i), s2.substr(0, i)) && isScramble(s1.substr(i, len1 - i), s2.substr(i, len2 - i)); ret = ret || isScramble(s1.substr(0, i), s2.substr(len2 - i, i)) && isScramble(s1.substr(i, len1 - i), s2.substr(0, len2 - i)); if (ret) { return true; } } return false; } };
2、动态规划法:
class Solution { public: bool isScramble(string s1, string s2) { if (s1.length() != s2.length()) { return false; } int length = s1.length(); // dp[i][j][k] means whether s1.substr(i, k) is scramble of s2.substr(j, k) vector<vector<vector<bool>>> dp(length, vector<vector<bool>>(length, vector<bool>(length + 1, false))); for (int k = 1; k <= length; ++k) { for (int i = 0; i + k - 1 < length; ++i) { for (int j= 0; j + k - 1 < length; ++j) { if (k == 1) { dp[i][j][k] = (s1[i] == s2[j]); continue; } else { for (int p = 1; p < k; ++p) { int length1 = p, length2 = k - p; if (dp[i][j][length1] && dp[i + length1][j + length1][length2]) { dp[i][j][k] = true; break; } if (dp[i][j + length2][length1] && dp[i + length1][j][length2]) { dp[i][j][k] = true; break; } } } } } } return dp[0][0][length]; } };
相关文章推荐
- [leetcode] 87. Scramble String 解题报告
- Leetcode 87. Scramble String 字符交换 解题报告
- [LeetCode] Scramble String 解题报告
- [LeetCode] Scramble String 解题报告
- [LeetCode] Scramble String 解题报告
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
- 【LeetCode】Scramble String 解题报告
- LeetCode 87. Scramble String
- leetcode 87: Scramble String
- LeetCode 87 Scramble String
- [leetcode-87]Scramble String(java)
- LeetCode解题报告--String to Integer (atoi)
- leetcode[87]Scramble String
- leetcode解题报告 Reverse Words in a String
- 87. Scramble String Leetcode Python
- 【LeetCode】String to Integer (atoi) 解题报告
- [LeetCode] String to Integer (atoi) 解题报告
- LeetCode: String to Integer (atoi) 解题报告
- 【LeetCode】87. Scramble String