[Leetcode] Interleaving string interlaced with the wrong strings

Source: Internet
Author: User






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



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



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






This is a cross-linked string and the word break split the words of the topic very similar, I said before, as long as it is encountered in the string of sub-sequences or matching problems directly on the dynamic planning dynamically programming, others do not consider, what recursion is a cloud, Painstakingly wrote recursive results to get OJ on the time Limit exceeded, can put the popularity faint, so still directly consider DP solution easier. In general, the string matching problem is to update a two-dimensional DP array, the core is to find the recursive formula. So let's start with the example given in the topic, and manually write out the two-dimensional array dp as follows:





 
 Ø d b b c a
Ø T F F F F F a T F F F F F
a T T T T T F
b F T T F T F
c F F T T T T
c F F F T F T





First, the premise of this problem is that the length of the string S1 and S2 must equal the length of the S3, and if not equal, it will definitely return false. Then, when S1 and S2 are empty strings, S3 is bound to be an empty string, which returns True. So directly to Dp[0][0] assignment true, and then if S1 and S2 one of them is an empty string, then the other is equal to the length of the S3, then the bitwise comparison, if the same, assign true, different assignment false, so the edge of the two-dimensional array dp is initialized well. The following is just to find the recursive formula to update the entire array, we found that at any non-edge position dp[i][j], its left or top may be true or false, both sides can be updated, as long as there is a way through, then this point can be true. Then we have to see separately, if the left is true, then we remove the current corresponding S2 in the string s2[j-1] and S3 in the corresponding position of the character compared to (calculate the corresponding position also consider the matching S1 characters), for S3[j-1 + i], if equal, then the true, Instead, it is assigned false. The same is true for the top, so you can find the recursive formula:



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



According to the above analysis, the code can be written as follows:





 
 
class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s1.size() + s2.size() != s3.size()) return false;
        int n1 = s1.size();
        int n2 = s2.size();
        vector<vector<bool> > dp(n1 + 1, vector<bool> (n2 + 1, false)); 
        dp[0][0] = true;
        for (int i = 1; i <= n1; ++i) {
            if (s1[i - 1] == s3[i - 1]) dp[i][0] = true;
        }
        for (int i = 1; i <= n2; ++i) {
            if (s2[i - 1] == s3[i - 1]) dp[0][i] = true;
        }
        for (int i = 1; i <= n1; ++i) {
            for (int j = 1; j <= n2; ++j) {
                dp[i][j] = (dp[i - 1][j] && s1[i - 1] == s3[i - 1 + j]) || (dp[i][j - 1] && s2[j - 1] == s3[j - 1 + i]);
            }
        }
        return dp[n1][n2];
    }
};
 

 





[Leetcode] Interleaving string interlaced with the wrong strings


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.