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. [设计模式] javascript 之 享元模式;

    享元模式说明 定义:用于解决一个系统大量细粒度对象的共享问题: 关健词:分离跟共享: 说明: 享元模式分单纯(共享)享元模式,以及组合(不共享)享元模式,有共享跟不共享之分:单纯享元模式,只包含共享的 ...

  2. RelativeLayout布局

    RelativeLayout用到的一些重要的属性: 第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:layout_center ...

  3. 贝叶斯决策_bayes(新闻分类)

    1.简单例子引入 2.先验概率 3.后验概率 4.最小错误率决策 5.最小风险贝叶斯决策 1. 贝叶斯公式 2简单例子 正常情况下,我们可以快速的将街上的人分成男和女两类.这里街上的人就是我们观测到的 ...

  4. 大型网站SEO优化策略框架

  5. Code First05--CodeFirst中值对象

    今天主要介绍EF Code First中一个高级部分:Value Object,中文翻译过来叫做值对象. 所谓的值对象就是一些没有生命周期,也没有业务逻辑上唯一标识符的类.哪些类是Entity,哪些类 ...

  6. html5的触摸事件

    1.触摸事件有哪些 touchstart,touchmove,touchend 2.分别什么时候触发 touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发. touc ...

  7. cocos2d界面渲染

    渲染是visit函数来做的, visit是先将不可见的节点和他所有的子节点都跳过, 然后再看节点的子节点是否为空, 如果为空的话直接看这个节点是否在摄像机可见范围之内, 如果在就渲染这个节点, 否则什 ...

  8. style="visibility: hidden" 和 style=“display:none”区别

    大多数人很容易将CSS属性display和visibility混淆,它们看似没有什么不同,其实它们的差别却是很大的. visibility属性用来确定元素是显示还是隐藏的,这用visibility=& ...

  9. opencv统计二值图黑白像素个数

    #include "iostream" #include "queue" #include "Windows.h" #include < ...

  10. c语言数据问题

    变量都有作用域,链接属性,和存储类型3个属性,这三个属性决定了变量的作用域和生存期的问题 在c语言中包含4中类型, 整形 浮点型 指针 聚合类型(数组,结构体等) ------------------ ...