LeetCode 97: Interleaving String staggered, flip String leetcode

Source: Internet
Author: User

LeetCode 97: Interleaving String staggered, flip String leetcode

Blog reprint please note address: http://blog.csdn.net/SunliyMonkey/article/details/48165711

Description

Address: https://leetcode.com/problems/interleaving-string/
Three strings S1 , S2 , S3 , Judge S3 Whether S1 , S2 In combination.

Example:

S1 = aabcc
S2 = dbbca
When S3 = aadbbcbcac Returns true.
When S3 = aadbbbaccc Returns false.

Site trap

None

Code
class Solution {public:    bool isInterleave(string s1, string s2, string s3) {       vector <int> match[2];        int cur, nxt;        int l;        int l1 = s1.length();        int l2 = s2.length();        int l3 = s3.length();        if(l1+l2 != l3)            return false;        int *vis[2];        vis[0] = new int[l1+1];        vis[1] = new int[l1+1];        for(int i = 0; i <= l1; i++)            vis[0][i] = vis[1][i] = -1;        nxt = 0;        match[0].push_back(0);        for(int i = 0; i < l3; i++){            cur = nxt;            nxt = !nxt;            match[nxt].clear();            for(int k = 0; k < match[cur].size(); k++){                l = match[cur][k];                if(l < l1 && s1[l] == s3[i]){                    if(vis[nxt][l+1] != i){                        match[nxt].push_back(l+1);                        vis[nxt][l+1] = i;                    }                }                if(i-l+1 <= l2 && s2[i-l] == s3[i]){                    if(vis[nxt][l] != i){                        match[nxt].push_back(l);                        vis[nxt][l] = i;                    }                }            }        }        return !match[nxt].empty();    }};
Ranking

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Blog address: http://blog.csdn.net/sunliymonkey

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.