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 swaps its-children, it produces a scrambled string "rgeat"
.
Rgeat / RG eat/\ / r G e at / a t
We say is "rgeat"
a scrambled string of "great"
.
Similarly, if we continue to swap the children of nodes "eat"
"at"
and, 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.
Determines whether two strings of S1 and S2 can be implemented by rotation.
Idea: Start dumbfounded, reaction not come over. The next day dawned, with recursion, divided the left and right parts, into sub-problems. But at first I thought that there was only one way to divide the tree, then found that the tree can be arbitrarily divided, the left subtree can have any character, right subtree is also. So you need to iterate through all the conditions. Then it is possible that the left half of the S1 is in the left half or the right half of the S2, so consider both of these situations.
When the loop is truncated, if the characters on both sides have inconsistent directly skip the cycle.
#include <iostream>#include<vector>#include<algorithm>#include<queue>#include<stack>#include<string>using namespacestd;classSolution { Public: BOOLIsscramble (stringS1,stringS2) { if(S1.length ()! =s2.length ())return false; if(S1 = =S2)return true; if(!isequal (S1, S2)) { return false; } for(inti =1; I < s1.length (); i++) { intLeftlength =i; intRightlength = S1.length ()-leftlength; stringS1LEFT1 = S1.substr (0, leftlength); stringS2LEFT1 = S2.substr (0, leftlength); stringS1RIGHT1 =s1.substr (Leftlength, rightlength); stringS2RIGHT1 =s2.substr (Leftlength, rightlength); stringS1LEFT2 =s1.substr (Rightlength, leftlength); stringS1right2 = S1.substr (0, rightlength); if(!isequal (S1LEFT1,S2LEFT1) &&!isequal (S1LEFT2,S2LEFT1)) { Continue; } if((Isscramble (S1LEFT1, S2LEFT1) && isscramble (s1right1, s2right1)) | | (Isscramble (s1left2, S2LEFT1) &&isscramble (s1right2, s2right1))) { return true; } } return false; } //determines whether the letter composition of two strings is consistent BOOLIsEqual (stringS1,stringS2) { if(S1.length ()! =s2.length ())return false; for(inti =0; I < s1.length (); i++) { intLocate =S2.find (S1[i]); if(Locate = =string:: NPOs) { return false; } Else{s2.erase (S2.begin ()+locate); } } return true; }};intMain () {solution S; stringS1 ="Oatzzffqpnwcxhejzjsnpmkmzngneo"; stringS2 ="Acegneonzmkmpnsjzjhxwnpqffzzto"; //string S1 = "Gneo"; //string s2 = "Gneo"; BOOLAns =s.isscramble (S1, S2); return 0;}
The great God gives the solution of the authentic dynamic programming, very concise, to study well. But this method forget all the situation so the time is longer than mine, almost 150ms, mine is about 50ms.
Dp[i][j][l] means whether s2.substr (j,l) is a scrambled string of s1.substr (i,l) or not.
classSolution { Public: BOOLIsscramble (stringS1,stringS2) { intlen=s1.size (); BOOLdp[ -][ -][ -]={false}; for(inti=len-1; i>=0; i--) for(intj=len-1; j>=0; j--) {dp[i][j][1]= (s1[i]==S2[j]); for(intL=2; I+l<=len && j+l<=len;l++) { for(intn=1; n<l;n++) {//all cases dividing the left and right intervalsdp[i][j][l]|=dp[i][j][n]&&dp[i+n][j+n][l-n];//Left of S1 to the left of S2dp[i][j][l]|=dp[i][j+l-n][n]&&dp[i+n][j][l-n];//The left of S1 to the right of S2. } } } returndp[0][0][len]; }};
"Leetcode" Scramble String (Hard) ★