【leetcode】Interleaving String

Interleaving String

Given s1s2s3, find whether s3 is 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.

采用动态规划,假设dp[i][j]表示了s1的前i个元素(包括第i个元素)s1[1],s1[2].....s1[i-1],与s2的前j个元素(包括第j个元素)s2[1],s2[2]...s2[j-1]是否可以构成s3

有两种情况可以考虑,

如果dp[i-1][j]成立了,且s1[i-1]==s3[i+j-1],那么可以认为dp[i][j]成立

如果dp[i][j-1]成立了,且s2[j-1]==s3[i+j-1],也可以认为dp[i][j]成立

所以可以得到如下的递推式:

dp[i][j]=(dp[i-1][j]&&s1[i-1]==s3[i+j-1])||(dp[i][j-1]&&s2[j-1]==s3[i+j-1]);

 1 class Solution {
 2 public:
 3     bool isInterleave(string s1, string s2, string s3) {
 4
 5         int n1=s1.length();
 6         int n2=s2.length();
 7         int n3=s3.length();
 8
 9         if(n1+n2!=n3)
10         {
11             return false;
12         }
13
14         vector<vector<bool> > dp(n1+1,vector<bool>(n2+1,false));
15
16         dp[0][0]=true;
17
18         for(int i=1;i<=n1;i++)
19         {
20             dp[i][0]=dp[i-1][0]&&s1[i-1]==s3[i-1];
21         }
22
23         for(int j=1;j<=n2;j++)
24         {
25             dp[0][j]=dp[0][j-1]&&s2[j-1]==s3[j-1];
26         }
27
28         for(int i=1;i<=n1;i++)
29         {
30             for(int j=1;j<=n2;j++)
31             {
32                 dp[i][j]=(dp[i-1][j]&&s1[i-1]==s3[i+j-1])||(dp[i][j-1]&&s2[j-1]==s3[i+j-1]);
33             }
34         }
35
36         return dp[n1][n2];
37     }
38 };
时间: 2024-10-11 07:27:55

【leetcode】Interleaving String的相关文章

【LeetCode】Interleaving String 解题报告

[题目] Given s1, s2, s3, find whether s3 is 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. [解析] 题意:有

【leetcode】 Interleaving String (hard)

Given s1, s2, s3, find whether s3 is 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. 思路:做过几道动态规划了,终于也有了

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【Leetcode】Reverse String

题目链接:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 思路: easy 算法: public String reverseString(String s) { char

【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer 题意:完成内置函数atoi的功能,将字符串转换成整数. 教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理.然后按照C++ Reference中关于atoi的规定一条一条写,才AC.另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以

【leetcode】Scramble String

Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string,

【LeetCode】8. String to Integer (atoi) 字符串转整数

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

【LeetCode】8 - String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode】443. String Compression

题目如下: Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying th