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.

思路:做过几道动态规划了,终于也有了点感觉。一次就AC了,好开心啊~

用dp[m][n]来存储s3[0 ~ m+n-1]是否是s1[0~m-1]与s2[0~n-1]交替组成的。注意,没有必要存s3的维度,因为s1[0~m-1]与s2[0~n-1]只能匹配s3的m+n个字符,即第三个维度是确定的。

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]

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

【leetcode】 Interleaving String (hard)的更多相关文章

  1. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

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

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】984. String Without AAA or BBB 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...

  5. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  6. 【leetcode】 Scramble String (hard)★

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

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

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  8. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  9. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

随机推荐

  1. AngularJS 使用ngOption实现下拉列表

    最近使用到了ngOption实现下拉选择列表,由于需要实现分组等功能,百度了下没有太好的文章,就百度到一篇英文的帖子,按照其中的代码很顺利的搞定了. 本篇根据文中代码,详细讲述下如何实现下拉列表 更多 ...

  2. 【开源一个小工具】一键将网页内容推送到Kindle

    最近工作上稍微闲点,这一周利用下班时间写了一个小工具,其实功能挺简单但也小折腾了会. 工具名称:Simple Send to Kindle Github地址:https://github.com/zh ...

  3. Ruby学习之module

    我们可以认为module是一个专门存放一系列方法和常量的工具箱. module和class非常像, 只是module不能创建实例也不能有子类, 它们仅仅能存放东西. 例如: module Circle ...

  4. 【C语言入门教程】4.1 一维数组

    数组与指针涉及到数据在内存中的存储位置问题,数组由连续的存储单元组成,最低地址对应于数组的第一个单元,最高地址对应于数组的最后一个单元.指针是一种特殊的变量,该变量所存放的是内存地址,通过指针变量可访 ...

  5. 在Xcode6.4中使用OpenCV

    XCode版本6.4,OpenCV版本3.0.0 昨天我安装完OpenCV之后,兴奋地按照这篇文章Mac平台上OpenCV开发环境搭建的步骤,在XCode上建了一个Demo工程,结果编译一直不成功.一 ...

  6. Oracle添加数据报文字与格式字符串不匹配错误

    今天在学习Oracle时碰到一个错:文字与格式字符串不匹配. 我在Oracle数据库中创建了一张表: --创建员工表employee create table employee ( empon ) n ...

  7. js面试题

    前几天在学习js的时候,碰到了这样一道面试题,要求计算出给你一个随机乱敲的一个字符串,要求在其中找出那个字符出现的次数最多,以及出现的个数. 这你有两种方案,请大家仔细阅读,有可能在你将来的面试中会碰 ...

  8. Javascript高级程序设计——执行环境与作用域

    Javascript中执行环境是定义了变量或函数有权访问的其他数据,决定了各自的行为,每个执行的环境都有一个与之关联的变量对象,环境中定义的所以变量和函数都保存在这个对象中. 全局执行环境是最外围的一 ...

  9. 利用LruCache为GridView加载大量本地图片完整示例

    MainActivity如下: package cc.testlrucache; import android.os.Bundle; import android.widget.GridView; i ...

  10. 创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三)

    1. 打开Eclipse,右键New JBPM Project 项目,在项目名称里面填写一个项目名字,如“TestJbpm6”,然后下一步,知道Finish完成(里面会使用我们配置的运行环境). 2. ...