Leetcode | | 97, interleaving String

Source: Internet
Author: User

Problem:

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.

Hide TagsDynamic Programming StringTest instructions: Can be interspersed with a third string in two string order

Thinking:

(1) First think of recursion, commit timeout.

(2) The correct solution of this problem is two-dimensional DP, state transfer equation:

Very good problem solving: http://www.cnblogs.com/remlostime/archive/2012/11/25/2787297.html

F[I][J] = (F[i][j-1] && s2[j-1] = = s3[i+j-1]) | | (F[i-1][j] && s1[i-1] = = s3[i+j-1]);

Only S3 's i+i-1 character and S1 's i-1 character or S2 's j-1 character typeface can be valid at the same time. Also refer to the previous results.

Code

Dp:

Class Solution {private:    bool f[1000][1000];p ublic:    bool Isinterleave (string s1, String s2, string s3) {        if ( S1.size () + s2.size ()! = S3.size ())            return false;                    F[0][0] = true;        for (int i = 1; I <= s1.size (); i++)  //f[i][0] Initialize            f[i][0] = f[i-1][0] && (s3[i-1] = = s1[i-1]);                    for (int j = 1; J <= S2.size (), j + +)  //f[0][j] Initialize            f[0][j] = f[0][j-1] && (s3[j-1] = = s2[j-1]);                    for (int i = 1; I <= s1.size (), i++) for            (int j = 1, J <= S2.size (); j + +)                f[i][j] = (F[i][j-1] && s2 [J-1] = = s3[i+j-1]) | | (F[i-1][j] && s1[i-1] = = s3[i+j-1]);                        Return F[s1.size ()][s2.size ()];}    ;

Recursion: Timeout

Class Solution {public:    bool Isinterleave (string s1, String s2, string s3) {        s1.push_back (' 0 ');        S2.push_back (' 0 ');        S3.push_back (' 0 ');        BOOL Flag=false;        Check (s1,0,s2,0,s3,0,flag);        return flag;    } Protected:    void Check (string s1,int i,string s2,int j,string s3,int K, bool &flag)    {        if (flag)            return;        if (s1[i]== ' 0 ' &&s2[j]== ' 0 ' &&s3[k]== ' 0 ')        {            flag=true;            return;        }        if (s1[i]== ' 0 ' &&s2[j]== ' 0 ' &&s3[k]!= ' 0 ')            return;        if ((s1[i]!= ' 0 ' &&s2[j]== ' 0 ' &&s3[k]== ' 0 ') | | (s1[i]== ' 0 ' &&s2[j]!= ' 0 ' &&s3[k]== ' 0 '))            return;        if (S1[i]!=s3[k]&&s2[j]!=s3[k])            return;        if (S1[i]==s3[k])            check (s1,i+1,s2,j,s3,k+1,flag);        if (S2[j]==s3[k])            check (S1,i,s2,j+1,s3,k+1,flag);      };


Leetcode | | 97, interleaving 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.