"Leetcode" interleaving String Problem Solving report

Source: Internet
Author: User

Topic

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.

Resolution

Test instructions: There are two strings, S1 and S2, each time a character is taken from one of the two strings (from the go to the back), until two strings are taken, so that many new strings can be formed, asking whether S3 belongs to one of these newly formed strings.

Violent recursion is the most straightforward idea, but it will time out.

The key to dynamic programming is to find the equation of motion, in other words, how to construct a two-dimensional array, and a two-dimensional array dp[i][j] to indicate what it means.

The following code, Dp[i][j], indicates whether the first I characters of S2 and the first J characters of S1 match the S3 first i+j characters .

Initialize dp[0][0]=0,dp[0][j] means that S2 takes 0, that is, whether the first J characters of S1 match the first J characters of S3, Dp[i][0] means S1 takes 0, that is, the first I character of S2, matches the first I character of S3.

Equation of motion: dp[i][j] = dp[i-1][j] && s2[i-1] = = s3[i + J-1] | | DP[I][J-1] && s1[i][j-1] = = S3[i + j-1], meaning if S2 's first i-1 characters and S1 's first J characters have been matched with S3 's first i+j-1 characters and S2 's I character equals S3 's i+j character, or S2 The first I characters of the S1 and the first j-1 characters of the string are already matched with the S3 I+j-1 characters, and the J character of S1 equals S3 's i+j character, then the first I character of S2 and the first J characters of S1 can be matched with S3 's first i+j-1 characters.

"Java Code"

public class Solution {public Boolean Isinterleave (string s1, String s2, string s3) {if (S1.length () + S2.leng                Th () = S3.length ()) return false;        Boolean[][] dp = new Boolean[s2.length () + 1][s1.length () + 1];        Dp[0][0] = true;  for (int j = 1; J <= S1.length (); j + +) {Dp[0][j] = Dp[0][j-1] && (S1.charat (j-1) = = S3.charat (j        -1)); } for (int i = 1; I <= s2.length (); i++) {dp[i][0] = dp[i-1][0] && (S2.charat (i-1) = = S3        . CharAt (i-1));                } for (int i = 1; I <= s2.length (), i++) {for (int j = 1; J <= S1.length (); j + +) { DP[I][J] = Dp[i-1][j] && (S2.charat (i-1) = = S3.charat (i + j-1)) | |            Dp[i][j-1] && (S1.charat (j-1) = = S3.charat (i + j-1));    }} return Dp[s2.length ()][s1.length ()]; }}

Reference Https://leetcode.com/discuss/22726/dp-solution-in-java


"Leetcode" interleaving String Problem Solving report

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.