leetcode#87 Scramble String

Source: Internet
Author: User

Original title Address

Two strings what are the criteria to be scramble?

If the length of S1 and S2 is equal to 1, it is clear that only s1=s2 is the scramble relationship.

If the length of the S1 and S2 is greater than 1, then the S1 and S2 are segmented and divided into two sub-problems to be processed separately.

How to split it? Of course, can not be arbitrarily split. Assuming that the S1 became S11 and s12,s2 into S21 and S22 after the split, there are only 2 ways to divide it:

1. s11.length = s21.length & s12.length = S22.length, as shown in:

S1:?????
----- ---
S11 S12

S2:?????
----- ---
S21 S22

2. S11.length = s22.length & s12.length = S21.length, as shown in:

S1:?????
----- ---
S11 S12

S2:?????
--- -----
S21 S22

After splitting, we can get two sets of substrings of equal length, which is exactly two sub-problems. So it is possible to split it recursively until the length of the two string is 1 stop splitting.

Make Scramblep[i][j][k] represent s1[i. I+k] and S2[j. J+k] is the scramble relationship between each other, that is, I is the starting position of the substring of S1, J is the starting position of the substring of S2, K is the length of the substring. Then there is the following recursive formula:

Scramblep[i][j][k] = (Scramblep[i][j][t] && scramblep[i+t][j+t][k-t]) | | (Scramblep[i][j+k-t][t] && scramblep[i+t][j][k-t]), where 0 < T < K

(which corresponds to the two methods mentioned above)

The initial value is: scramblep[i][j][k] = s1[i. I+k] = = S2[j. J+k], (if two substrings are equal, the scramble relationship is natural.) This initial value contains the case of a length of 1)

Code:

1 BOOLIsscramble (stringS1,stringS2) {2   if(S1.length ()! = S2.length ())return false;3   if(S1.empty ())return true;4 5   intLen =s1.length ();6   BOOLJudge =New BOOL**[Len];7    for(inti =0; i < Len; i++) {8Judge[i] =New BOOL*[Len];9      for(intj =0; J < Len; J + +)TenJUDGE[I][J] =New BOOL[Len +1]; One   } A  -    for(intK =1; K <= Len; k++) { -      for(inti =0; i + k <= len; i++) { the        for(intj =0; J + k <= Len; J + +) { -Judge[i][j][k] = S1.substr (i, k) = =S2.substr (j, K); -          for(intt =1; T < K &&!judge[i][j][k]; t++) { -Judge[i][j][k] |= judge[i][j + k-t][t] && judge[i + t][j][k-T]; +JUDGE[I][J][K] |= judge[i][j][t] && judge[i + t][j + t][k-T]; -         } +       } A     } at   } -  -   returnjudge[0][0][len]; -}

New memory is not delete, do not forget the interview.

leetcode#87 Scramble String

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.