Given A string S1, we may represent it as a binary tree by partitioning it to the Non-empty substrings Recursivel Y.
Below is one possible representation of S1 = "great" :
Great / gr eat/\ / g r E at / a t
To scramble the string, we are choose any non-leaf node and swap it to the children.
for example, if we choose the Node " GR " and swap its-children, it produces a scrambled string < Code style= "Font-family:menlo,monaco,consolas, ' Courier New ', monospace; font-size:12.6000003814697px; PADDING:2PX 4px; Color:rgb (199,37,78); Background-color:rgb (249,242,244) ">" 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 is "rgtae" a scrambled string of "great" .
Given strings S1 and S2 of the same length, determine if S2 is a scrambled string of S1 .
Basic ideas:
Moving too planned
Two strings s1, S2, truncated to two segments in the right place. Is not scramble, consists of the following two kinds of circumstances.
-----******** and-----********
-----******** and ********-----
Where truncation occurs, a position-by-location test is performed. This method is violent, and the only thing that is different from violence is to store the operations that have been done in the past and avoid duplication.
A three-dimensional array is required to store the results of the intermediate operation.
Dp[i][j][len] Store, S1 string, from the first position of the beginning, Len length of the substring, and S2 string, starting from the first J position, Len length of the string, whether it is scramble.
The final target, which requires a value of dp[0][0][s1.size ()].
In order to find out Dp[i][j][len], it is necessary to gradually test, will dp[i][j][len] the substring represented by sub-string segmentation, that is, to proceed from position 1. Len-1 separate the trial.
The recursive type is
Dp[i][j][len-1] |= dp[i][j][k-1] && dp[i+k][j+k][len-k-1]; Dp[i][j][len-1] |= dp[i][j+len-k][k-1] && dp[i+k][j][len-k-1];
The first line, corresponding, 1.-----******** and-----********
The second line, corresponding, 2. -----******** and ********-----
This code actually runs at 106ms on Leetcode.
Class Solution {public: bool Isscramble (string s1, string s2) { if (s1.empty () && s2.empty ()) return true; if (s1.size () = S2.size ()) return false; const int size = S1.size (); vector<vector<vector<char> > > dp (Size, vector<vector<char> > (Size, vector<char> (size))); for (int i=s1.size ()-1, i>=0; i--) {for (int j=s2.size ()-1; j>=0; j--) { dp[i][j][0] = s1[i] = = S2[j]; for (int len=2, len<=size-i && len<=size-j; len++) {for (int k=1; k<len; k++) { dp[i][j][len-1] |= Dp[i][j][k-1] && dp[i+k][j+k][len-k-1]; Dp[i][j][len-1] |= dp[i][j+len-k][k-1] && dp[i+k][j][len-k-1]; }}} return dp[0][0][size-1];} ;
On Leetcode, there is only 6ms of code to use. But I don't understand why he was right to do that.
Https://leetcode.com/discuss/27889/my-accepted-solution-in-6ms-little-change-in-normal-solution
Scramble String--Leetcode