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"
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.
Example 1:
Input: s1 = "great", s2 = "rgeat"
Output: true
Example 2:
Input: s1 = "abcde", s2 = "caebd"
Output: false
题意
将字符串任意分解成为二叉树,将这棵二叉树的任意株子树进行分支的交换再次拼合而成的字符串中,是否包括s2
题解
速度一般般的递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public: 3 bool equal(vector<int>&a, vector<int>&b) { 4 for (int i = 0; i < a.size(); i++) 5 if (a[i] != b[i]) 6 return false; 7 return true; 8 } 9 bool isScramble(string s1, string s2) { 10 vector<int>c1(256, 0), c2(256, 0); 11 int l = s1.length(); 12 if (l <= 3) { 13 for (int i = 0; i < l; i++) 14 c1[s1[i]]++, c2[s2[i]]++; 15 if (equal(c1, c2))return true; 16 else return false; 17 } 18 for (int i = 0; i < l - 1; i++) { 19 c1[s1[i]]++, c2[s2[i]]++; 20 if (equal(c1, c2) && isScramble(s1.substr(0, i + 1), s2.substr(0, i + 1)) && isScramble(s1.substr(i + 1), s2.substr(i + 1))) 21 return true; 22 } 23 c1 = vector<int>(256, 0), c2 = vector<int>(256, 0); 24 for (int i = 0; i < l - 1; i++) { 25 c1[s1[i]]++, c2[s2[l-1-i]]++; 26 if (equal(c1, c2) && isScramble(s1.substr(0, i + 1), s2.substr(l-1-i)) && isScramble(s1.substr(i + 1), s2.substr(0,l-i-1))) 27 return true; 28 } 29 return false; 30 } 31 };
然后不知道是不是有意为之,数据的字符串里只包括小写字母,于是我改了一下提升了一点速度:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public: 3 bool equal(vector<int>&a, vector<int>&b) { 4 for (int i = 0; i < a.size(); i++) 5 if (a[i] != b[i]) 6 return false; 7 return true; 8 } 9 bool isScramble(string s1, string s2) { 10 vector<int>c1(26, 0), c2(26, 0); 11 int l = s1.length(); 12 if (l <= 3) { 13 for (int i = 0; i < l; i++) 14 c1[s1[i]-'a']++, c2[s2[i]-'a']++; 15 if (equal(c1, c2))return true; 16 else return false; 17 } 18 for (int i = 0; i < l - 1; i++) { 19 c1[s1[i]-'a']++, c2[s2[i]-'a']++; 20 if (equal(c1, c2) && isScramble(s1.substr(0, i + 1), s2.substr(0, i + 1)) && isScramble(s1.substr(i + 1), s2.substr(i + 1))) 21 return true; 22 } 23 c1 = vector<int>(256, 0), c2 = vector<int>(256, 0); 24 for (int i = 0; i < l - 1; i++) { 25 c1[s1[i]-'a']++, c2[s2[l-1-i]-'a']++; 26 if (equal(c1, c2) && isScramble(s1.substr(0, i + 1), s2.substr(l-1-i)) && isScramble(s1.substr(i + 1), s2.substr(0,l-i-1))) 27 return true; 28 } 29 return false; 30 } 31 };
然后我看了下其他人的递归做法发现根本不用存字符串包括了哪些字母
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public: 3 bool isScramble(string s1, string s2) { 4 int l = s1.length(); 5 if (s1 == s2)return true; 6 string str1 = s1, str2 = s2; 7 sort(str1.begin(), str1.end()); 8 sort(str2.begin(), str2.end()); 9 if (str1 != str2)return false; 10 for (int i = 0; i < l - 1; i++) { 11 string p1 = s1.substr(0, i + 1), p2 = s2.substr(0, i + 1), p3 = s1.substr(i + 1), p4 = s2.substr(i + 1); 12 if (isScramble(p1, p2) && isScramble(p3, p4))return true; 13 p2 = s2.substr(l - 1 - i), p4 = s2.substr(0, l - 1 - i); 14 if (isScramble(p1, p2) && isScramble(p3, p4))return true; 15 } 16 return false; 17 } 18 };
用dp好像更慢了,因为是三维的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // DP 2 class Solution { 3 public: 4 bool isScramble(string s1, string s2) { 5 if (s1.size() != s2.size()) return false; 6 if (s1 == s2) return true; 7 int n = s1.size(); 8 vector<vector<vector<bool> > > dp (n, vector<vector<bool> >(n, vector<bool>(n + 1, false))); 9 for (int i = 0; i < n; ++i) { 10 for (int j = 0; j < n; ++j) { 11 dp[i][j][1] = s1[i] == s2[j]; 12 } 13 } 14 for (int len = 2; len <= n; ++len) { 15 for (int i = 0; i <= n - len; ++i) { 16 for (int j = 0; j <= n - len; ++j) { 17 for (int k = 1; k < len; ++k) { 18 if ((dp[i][j][k] && dp[i + k][j + k][len - k]) || (dp[i + k][j][len - k] && dp[i][j + len - k][k])) { 19 dp[i][j][len] = true; 20 } 21 } 22 } 23 } 24 } 25 return dp[0][0][n]; 26 } 27 };