Leetcode-interleaving string

Source: Internet
Author: User

Given S1, S2, S3, find whether S3 is 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.

// Solve the problem by using DP. The DP [I] [J] State is represented as S1 [0... i] + S2 [0... if the string range of J] can constitute S3. //, the dynamic transfer equation is: // 1) s1 [I-1] = S3 [I + J-1] & DP [I-1] [J] = true then, DP [I] [J] = true; // 2) s2 [J-1] = S3 [I + J-1] & DP [I] [J-1] = true then DP [I] [J] = true; Class solution {public: bool isinterleave (STD: String S1, STD: String S2, STD: String S3) {If (s1.size () + s2.size ()! = S3.size () return false; STD: vector <bool> dp (s1.size () + 1, STD: vector <bool> (s2.size () + 1, 0); DP [0] [0] = 1; for (INT I = 1; I <s1.size () + 1; I ++) {If (S1 [I-1] = S3 [I-1] & DP [I-1] [0]) DP [I] [0] = true ;} for (INT I = 1; I <s2.size () + 1; I ++) {If (s2 [I-1] = S3 [I-1] & DP [0] [I-1]) DP [0] [I] = true ;} for (INT I = 1; I <s1.size () + 1; I ++) {for (Int J = 1; j <s2.size () + 1; j ++) {If (S1 [I-1] = S3 [I + J-1] & DP [I-1] [J]) DP [I] [J] = true; if (s2 [J-1] = S3 [I + J-1] & DP [I] [J-1]) DP [I] [J] = true ;}} return DP [s1.size ()] [s2.size ()] ;}};


Leetcode-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.