[Leetcode] [Java] Interleaving String

Source: Internet
Author: User

Title:

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.

Test Instructions:

Given s1, S2, S3, determine whether S3 is composed of S1 and S2 interleaving.

Like what

Given

 s1  =  " AABCC " BR style= "Color:rgb (51,51,51); line-height:30px "> s2  =  " DBBCA " ,

when   S3  =  " AADBBCBCAC ", Return to  true.

when S3  = "aadbbbaccc"时,返回 false.

Algorithm Analysis:

Reference Blog: http://pisxw.com/algorithm/Interleaving-String.html

http://blog.csdn.net/u011095253/article/details/9248073

This is a question about string manipulation, and the requirement is to determine whether a string can be constructed by two strings in their own order, picking up a single character from two strings at a time.

It is easy to think of a dynamic plan to accomplish a certain amount of problems, such as judging whether it is possible to follow a certain rule.

First talk about maintenance, res[i][j] means that with the S1 of the first I characters and S2 of the first J characters can be represented by the rules S3 the first i+j characters, so the final result is res[s1.length ()][s2.length ()] To determine whether it is true. The next step is recursion, assuming you know all the historical information before Res[i][j], how do we get res[i][j]. As can be seen, in fact there are only two ways to recursion, one is to choose S1 characters as S3 new characters, and the other is to choose S2 characters as the new characters. and to see if you can choose, is to Judge S1 (S2) of the first (j) characters and S3 I+j Word typeface, and so on. If it can be selected and the corresponding Res[i-1]j is true, the S3 I+j characters can be represented. As long as there is a form of these two cases, it means that res[i][j] is true and is a relationship. So a recursive expression can be represented as

res[i][j] = res[i-1][j]&&s1.charAt(i-1)==s3.charAt(i+j-1) || res[i][j-1]&&s2.charAt(j-1)==s3.charAt(i+j-1)

Time because it is a two-dimensional dynamic programming, so the complexity is O (m*n), M and n respectively is the length of S1 and S2

AC Code:

<span style= "Font-family:microsoft yahei;font-size:12px;" >public class Solution {public Boolean Isinterleave (string s1, String s2, string s3) {//Note:the Solu        tion object is instantiated only once and are reused by each test case.        if (S1 = = NULL | | s2 = = NULL | | s3 = = NULL) return false;        if (s1.length () + s2.length () = S3.length ()) return false;        Boolean[][] dp = new Boolean[s1.length () + 1][s2.length () + 1];        Dp[0][0] = true; for (int i = 1; i < s1.length () + 1; i++) {if (S1.charat (i-1) = = S3.charat (i-1) && dp[i        -1][0]) dp[i][0] = true; } for (int j = 1; J < S2.length () + 1; j + +) {if (S2.charat (j-1) = = S3.charat (j-1) &AMP;&A mp        DP[0][J-1]) Dp[0][j] = true;            } for (int i = 1; i < s1.length () + 1, i++) {for (int j = 1; J < S2.length () + 1; j + +) {if (S1.charat (i- 1) = = S3.charat (i + j-1) && dp[i-1][j]) | |                    (S2.charat (j-1) = = S3.charat (i + j-1) && dp[i][j-1]))            DP[I][J] = true;    }} return Dp[s1.length ()][s2.length ()]; }}</span>


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] 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.