Interleaving String
https://leetcode.com/problems/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.
这题有点类似于之前的求编辑距离的题:http://www.cnblogs.com/biyeymyhjob/archive/2012/09/28/2707343.html
定义f[i][j]为 以s1[0,i]s2[0,j]匹配s3[i][j]
当j==0时,f[i][0]= f[i-1][0] && s1[i-1]==s3[i-1]
当i==0时,f[0][j]= f[0][j-1] && s2[j-1]==s3[j-1]
当i>=1&&j>=1时,f[i][j]= (f[i-1][j] && s1[i-1]==s3[i-1][j]) || (f[i][j-1] && s2[j-1]== s3[i][j-1])
bool isInterleave(string s1, string s2, string s3) {
if(s3.length()!=s1.length()+s2.length())
return false;
vector<vector<bool>> f(s1.length()+,vector<bool>(s2.length()+,true));
for(int i=;i<=s1.length();i++)
f[i][]= f[i-][] && s1[i-]==s3[i-];
for(int i=;i<=s2.length();i++)
f[][i]=f[][i-] && s2[i-]==s3[i-];
for(int i=;i<=s1.length();i++)
{
for(int j=;j<=s2.length();j++)
{
f[i][j]=(f[i-][j] && s1[i-]==s3[i+j-]) || (f[i][j-] && s2[j-]==s3[i+j-]);
}
}
return f[s1.length()][s2.length()];
}
Interleaving String的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 二维动态规划——Interleaving String
97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
随机推荐
- SQLite3源程序分析之查询处理及优化
前言 查询处理及优化是关系数据库得以流行的根本原因,也是关系数据库系统最核心的技术之一.SQLite的查询处理模块很精致,而且很容易移植到不支持SQL的存储引擎(Berkeley DB最新的版本已经将 ...
- SQL 基础语法(一)
SQL 语法 数据库表 一个数据库通常包含一个或多个表.每个表由一个名字标识(例如"客户"或者"订单").表包含带有数据的记录(行). 下面的例子是一个名为 & ...
- 递推 hdu 2064
z[n] n个盘子从1到3次数 先想2个的时候 1->2 2->3 1->2 3->2 2->1 2->3 1->2 2->3 显然 要先把上面n- ...
- [转]Pythoin中的Lambda表达式
引用自:http://www.cnblogs.com/evening/archive/2012/03/29/2423554.html 在学习python的过程中,lambda的语法时常会使人感到困惑, ...
- c# Entity DbArithmeticExpression arguments must have a numeric common type
在Entity Lambda表达式中计算DateTime类型的时候 不能直接用DateTime类型进行相减计算差值 需要使用DbFunctions提供的一些方法. 例如: DbFunctions.Di ...
- 用hibernate tools生成对应的sql应用代码
参考资料: eclipse在线配置hibernate tools http://jingyan.baidu.com/article/db55b609959d154ba20a2f5d.html [图]H ...
- jQuery 运行机制
1.Selector,查找元素.这个查找不但包含基于CSS1~CSS3的CSS Selector功能,还包含其对直接查找或间接查找而扩展的一些功能. 2.Dom元素的属性操作.Dom元素可以看作htm ...
- css随笔1
1.简单清除浏览器样式 *{ padding: 0px; margin: 0px; } 2.得到屏幕范围的div html,body{ width: 1 ...
- UVA1625Color Lenth(DP+LCS变形 未AC)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/C 紫书P276 res[i][j]表示第一个序列移动i个,第 ...
- hg0088新2网址:已经做好了封装直接拿来就能用功能齐全
很简单,InkCanvas就不用多介绍了,它是一个面板,特点是你可以在它上面涂抹,就像大街上那些妖怪那样,把化妆品往脸上乱涂,涂得人不像人,鸡不像鸡. InkToolBar呢是一个现成的工具栏,你可以 ...