Interleaving String--leetcode

Source: Internet
Author: User

Given s1, S2, S3, find whether S3 are formed by the interleaving of S1 and s2.

For example,
Given:
S1 = "aabcc" ,
s2 = "dbbca" ,

When s3 = "aadbbcbcac" , return true.
When s3 = "aadbbbaccc" , return false.

Topic: Given three strings, S1,S2,S3, ask whether S3 is composed of S1 and S2 cross.

Solution thinking: Recursive timeout, repeated calculation too much, plus the cache may be.

Talk about the DP method, the study of a half-day to understand, with a two-dimensional Boolean array dp[i][j] to take the former I-bit from the S1, from the S2 to take the former J-Bits constitute the S3 of the former i+j bit can be.

Then Dp[i][j] may come from dp[i-1][j] or dp[i][j-1]:

If from dp[i-1][j], then dp[i-1][j] should be true, and S1.charat (i-1) =s3.charat (I+J-1) is established, dp[i][j]=true;

If from dp[i][j-1], then dp[i][j-1] should be true, and S2.charat (j-1) =s3.charat (I+J-1) is established, dp[i][j]=true;

Initialized, Dp[0][0]=true represents two empty strings that constitute an empty string of true.

In addition, the first n or M characters that form the S3 starting with S1 or S2 are also initial conditions.

For example, pay attention to the upper left corner of the pair of arrows pointing to the lattice dp[1][1], indicating S1 take 1th bit A, S2 take 1th bit D, whether it can make up the top two AA of S3

From dp[0][1] down the arrows, S1 currently take 0 bits, S2 currently take 1 bits, we add S1 1th bit, to see if it is not equal to S3 2nd bit, (i + j bit)

From dp[1][0] to the right arrow, S1 currently take 1 bits, S2 currently take 0 bits, we add s2 1th bit, see if it is equal to S3 2nd bit, (i + j bit)

 Public Static BooleanIsinterleave (string s1, String s2, string s3) {if(S3.length ()! = S1.length () +s2.length ()) {            return false; }        Boolean[] DP =New Boolean[S1.length () + 1] [S2.length () + 1]; dp[0][0] =true;  for(inti = 0; I < s1.length (); i++) {            if(S1.charat (i) = =S3.charat (i)) {Dp[i+ 1][0] = dp[i][0]; }        }         for(inti = 0; I < s2.length (); i++) {            if(S2.charat (i) = =S3.charat (i)) {dp[0][i + 1] = Dp[0][i]; }        }         for(inti = 1; I < S1.length () + 1; i++) {             for(intj = 1; J < S2.length () + 1; J + +) {Dp[i][j]= (Dp[i-1][j] && s1.charat (i-1) = = S3.charat (i + j-1)) | | (Dp[i][j-1] && s2.charat (j-1) = = S3.charat (i + j-1)); }        }        returndp[s1.length ()][s2.length ()]; }

Reference: http://blog.csdn.net/u011095253/article/details/9248073

Interleaving String--leetcode

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.