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 ...
随机推荐
- redis 学习笔记(6)-cluster集群搭建
上次写redis的学习笔记还是2014年,一转眼已经快2年过去了,在段时间里,redis最大的变化之一就是cluster功能的正式发布,以前要搞redis集群,得借助一致性hash来自己搞shardi ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Department Highest Salary 系里最高薪水
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- Redis系列之(二):Redis主从同步,读写分离
1. Redis主从同步 Redis支持主从同步.数据可以从主服务器向任意数量的从服务器上同步,同步使用的是发布/订阅机制. 2. 配置主从同步 Mater Slave的模式,从Slave向Maste ...
- 关于js解析的一点小问题
先来看一下下面的一段代码有什么问题? <html><head><script src="./jquery.min.js"></script ...
- Android 在线订单倒计时设计
接到一个需求,用户下单后,商店这边需要显示在线订单列表,订单十分钟内有效.于是需要设计倒计时,显示每个订单剩余处理时间. 倒计时剩余时间: 订单创建时间 + 10分钟 - 系统当 ...
- selenium 使用action进行鼠标,键盘操作
<!--test.html--> <html> <head> <title>Set Timeout</title> <script&g ...
- thinkphp vendor
vendor('weixin.request'); 对应文件:Core\Library\Vendor\weixin\request.php import('weixin.request#class') ...
- Spring,Mybatis 整合Memcache
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- .htaccess语法之RewriteCond与RewriteRule指令格式详细解释
htaccess语法之RewriteCond与RewriteRule指令格式详细解释 (2012-11-09 18:09:08) 转载▼ 标签: htaccess it 分类: 网络 上文htacc ...