The original title link is here: https://leetcode.com/problems/scramble-string/
This question refers to this post: http://blog.csdn.net/linhuanmars/article/details/24506703
This is actually a three-dimensional dynamic programming topic, we propose maintenance amount Res[i][j][n], where I is the starting character of S1, J is the starting character of S2, and N is the current string length, Res[i][j][len] The string representing the length of Len with the beginning of S1 and S2 respectively for I and J is not mutually scramble.
With maintenance, we'll look at recursion, which is how to get res[i][j][len by historical information]. Judging this is not satisfied, in fact we first is the current s1[i...i+len-1] string hack into two parts, and then divided into two cases: the first is left and S2[j...j+len-1] The left part is not scramble, as well as the right and s2[j...j+ Len-1] Right part is not scramble, the second case is left and s2[j...j+len-1] right part is not scramble, and right and s2[j...j+len-1] The left part is not scramble. If one of the above two cases is established, the explanation s1[i...i+len-1] and s2[j...j+len-1] is scramble. We have historical information as to whether these left and right parts are scramble, because all cases with a length of less than n are solved in front (that is, the length is the outermost loop).
Above said is the case of splitting a knife, for s1[i...i+len-1] we have len-1 seed splitting method, in these splitting method as long as there is a form, then two string is scramble.
Summing up recursion is res[i][j][len] = | | (Res[i][j][k]&&res[i+k][j+k][len-k] | | res[i][j+len-k][k]&&res[i+k][j][len-k]) for all 1<=k<len , that is, the result of all len-1 seed splitting method or operation. Because the information is computed, for each type of cleavage only a constant operation is required, so the solution recursion is required O (len) (because of the len-1 seed splitting method).
So total time complexity because it is a three-dimensional dynamic planning, requires three layers of loop, plus each step requires line line time to solve the recurrence, so is O (n^4). Although it is already relatively high, but at least not the order of magnitude, dynamic planning is still a big thing, the space complexity is O (n^3).
AC Java:
1 Public classSolution {2 Public Booleanisscramble (string s1, string s2) {3 if(S1 = =NULL|| S2 = =NULL|| S1.length ()! =s2.length ()) {4 return false;5 }6 if(s1.length () = = 0){7 return true;8 }9 intLen1 =s1.length ();Ten intLen2 =s2.length (); One Boolean[][][] dp =New Boolean[Len1] [Len2] [Len1+1]; A for(inti = 0; i<len1; i++){ - for(intj = 0; j<len2; J + +){ -DP[I][J][1] = S1.charat (i) = =S2.charat (j); the } - } - - for(intlen=2; len<=len1; len++){ + for(inti=0; i<len1-len+1; i++){ - for(intj=0; j<len2-len+1; J + +){ + for(intK = 1; k<len; k++){ ADp[i][j][len] |= (Dp[i][j][k] && dp[i+k][j+k][len-k]) | | (Dp[i][j+len-k][k] && dp[i+k][j][len-K]); at } - } - } - } - returnDp[0][0][len1]; - } in}
Leetcode Scramble String