[Leetcode] Scramble string String DP

Source: Internet
Author: User

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.

Hide TagsDynamic Programming String The topic began to think or is quite complex, the first idea is a binary tree search, all possible results are searched, and then if found is, can not find it is not, so write the words of speed need to consider, improve the method is depth lookup time to determine whether the number of characters in the input parameter is the same:
classSolution { Public:    BOOLIsscramble (stringS1,stringS2) {        intLen1 = S1.size (), len2 =s2.size (); if(Help_f (S1,S2)) {if(S1==S2)return true;  for(inti =1; i<len1;i++){                if(S1.substr (0, i) +s1.substr (i) ==s2)return true; }             for(intI=1; i<len1;i++){                if(Isscramble (S1.substr (0, i), S2.substr (0, i)) &&isscramble (S1.substr (i), s2.substr (i)))return true; //cout<<s1.substr (0,i) << "<<s2.substr (len2-i) <<" "<<s1.substr (len1-i) << "" <<s2.substr (0,i) <<endl;                if(Isscramble (S1.substr (0, i), S2.substr (len2-i)) &&isscramble (S1.substr (i), S2.substr (0, len2-i)))return true; }        }        return false; }    BOOLHelp_f (string&AMP;S1,string&S2) {        if(S1.size ()!=s2.size ())return false; intc[ -]={0};  for(intI=0; I<s1.size (); i++) c[s1[i]-'a'] ++;  for(intI=0; I<s2.size (); i++) {C[s2[i]-'a']--; if(c[s2[i]-'a']<0)return false; }        return true; }};

The second idea is dynamic planning, set Table[i][j][len],i J as the starting position of the string S1 s2, Len is the length to be considered, if S1 I to i + Len and S2 's J to J+len conform, true, within the length range, traverse each Location: Tab[i][j][len] |= tab[i][j][l] && tab[i+l][j+l][len-l] or Tab[i][j][len] |= tab[i][j+len-l][l] && Tab[i+l][j][len-l]
classsolution{ Public:    BOOLIsscramble (stringS1,stringS2) {        intLen1=s1.size (), len2=s2.size (); if(LEN1!=LEN2)return false; BOOLtable[ -][ -][ -]={false};  for(intI=len1-1; i>=0; i--){             for(intJ=len1-1; j>=0; j--) {table[i][j][1]= (s1[i]==S2[j]);  for(inttmplen=2; i+tmplen<=len1&&j+tmplen<=len1;tmplen++){                     for(intidx=1; idx<tmplen;idx++) {Table[i][j][tmplen]|=table[i][j][idx]&&table[i+idx][j+idx][tmplen-IDX]; Table[i][j][tmplen]|=table[i][j+tmplen-idx][idx]&&table[i+idx][j][tmplen-IDX]; }                }            }        }        returntable[0][0][LEN1]; }};

[Leetcode] Scramble string String DP

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.