Leetcode 97. Interleaving String? Staggered character discriminant problem solving report

Source: Internet
Author: User
1 ideas for solving problems

My problem? is to refer to this blog: Portal

Specifically, this question is to let us judge whether the string S3 by the characters in S1 and S2? That is, the S1 of each character is kept in a relatively orderly situation randomly inserted into the S2 whether can get S3 (described here, S1 and S2 can be interchangeable)

First of all, the traditional recursive way will certainly time out, certainly not.

The approach here is to expand the S1 and S2 into a matrix, assuming that the length is n,m, then construct a n*m DP matrix and use the DP method to judge.

Specifically (from the original text):

When the S1 arrives at the first element, the S2 arrives at the J element:

The right one step on the map is s2[j-1] matching s3[i+j-1].

One step down on the map is s1[i-1] matching s3[i+j-1].

Example: s1= "AA", s2= "AB", s3= "Aaba". The Mark 1 is feasible. Finally returns to the lower right corner.

0  a  b

0 1 1 0

A 1 1 1

A 1 0 1 2 original question

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

For example,
Given:
S1 = "AABCC",
s2 = "DBBCA", while

s3 = "AADBBCBCAC", return true.
When s3 = ' AADBBBACCC ', return false.
3 AC Solution
public class Solution {//public boolean Isinterleave (string s1, String s2, string s3) {int n=s1==null?0
        : S1.length ();
        int M=s2==null?0:s2.length ();
        int K=s3==null?0:s3.length ();
        if (n+m!= k) return false;
        char[] S1 = S1.tochararray ();
        char[] S2 = S2.tochararray ();
        char[] S3 = S3.tochararray ();
        Boolean map[][]=new boolean[n+1][m+1]; According to the method described above, the state for (int i=0;i<=n;i++) {for (int j=0;j<=m;j++) {if (i==0 &&amp) is judged. ;
                j==0) {Map[i][j] = true;
                else if (i==0) {map[i][j] = Map[i][j-1] & s3[i+j-1]==s2[j-1];
                else if (j==0) {map[i][j] = Map[i-1][j] & s3[i+j-1]==s1[i-1]; else {Map[i][j] = (Map[i][j-1] & S3[i+j-1]==s2[j-1]) |
                (Map[i-1][j] & s3[i+j-1]==s1[i-1]);
}} return map[n][m];

    }
} 

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.