[Leetcode] Scramble String--an example of three-dimensional dynamic programming

Source: Internet
Author: User

(Version 0.0)

As a small weak, this topic is my first time to encounter three-dimensional dynamic planning. When you do it, realize that the so-called scramble actually have two possible types, one is the two sub-nodes in the lower layer of the swap, such as if we from the first layer of segmentation point, or from the higher layer of the segmentation point of view, The substring on the left side of the S1 and S2 points should have exactly the same number of characters, the same right is identical, and the other is the interchange at the higher-level segmentation point, so that if we examine S1 and S2 on the same level, You will find that the char type on the right side of the Shard point on the left side of the S1 and S2 is the same as the number of char, S1 to the right of the Shard point and to the left of the S2 shard point. So we need to consider the state transfer in addition to the length of the substring involved, but also involves the respective starting position of the substrings in S1 and S2, so we need to maintain a three-dimensional DP array to store the information. This is a reference to the blog of code Ganker, the following code is slightly different from its code, personally think better understand some.

1  Public classSolution {2      Public Booleanisscramble (string s1, string s2) {3         if(S1.length ()! =s2.length ()) {4             return false;5         }6         if(s1.equals (S2)) {7             return true;8         }9         intLen =s1.length ();Ten         Boolean[][][] lenscramble =New Boolean[Len][len][len]; One          for(inti = 0; i < Len; i++) { A              for(intj = 0; J < Len; J + +) { -LENSCRAMBLE[0][I][J] = (S1.charat (i) = =S2.charat (j)); -             } the         } -          for(intL = 2; L <= Len; l++) { -             intbound = Len-l; -              for(inti = 0; I <= bound; i++) { +                  for(intj = 0; J <= Bound; J + +) { -                      for(intK = 1; K < L; k++) { +                         intL2 = L-K; A                         if((Lenscramble[k-1][i][j] && lenscramble[l2-1][i + k][j +K]) at|| (Lenscramble[k-1][i][j + L2] && Lenscramble[l2-1][i +K] [j])) { -LENSCRAMBLE[L-1][I][J] =true; -                              Break; -                         } -                     } -                 } in             } -         } to         returnLenscramble[len-1][0][0]; +     } -}

Here the Lenscramble[l][i][j] represents the length of the S1 from the I position in the beginning of the substring and S2 from the J position substring is the scramble of each other string. State transfer is more straightforward, is to enumerate the possible segmentation points, and then examine the two cases mentioned above whether there is at least one, if it is established can immediately set the corresponding element to true and then break out of the loop.

Overall the idea of this problem is actually more conventional, but if it is the first time to see three-dimensional DP words may be in how to deal with the state transfer, and I think in the first time when the situation may be transferred in the State to consider the two cases mentioned above, but because have not seen three-dimensional DP, Always feel as if their thinking is too complex to OJ. From this point of view, this question is quite meaningful for broadening the range of the subject matter.

[Leetcode] Scramble String--an example of three-dimensional dynamic programming

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.