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 ...
随机推荐
- codevs 3110 二叉堆练习3
3110 二叉堆练习3 http://codevs.cn/problem/3110/ 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 I ...
- 【笔记】JS基础一
名词解释 ECMAScript 翻译器,解释器 DOM 文档对象模型 Document Object Model 例如 document BOM 浏览器对象模型 Browser Object Mode ...
- js获取可视区域高度
document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...
- caffe源码解析
http://blog.csdn.net/lanxuecc/article/details/53186613
- Android基础总结(四)
网络图片查看器 确定图片的网址 发送http请求 URL url = new URL(address); //获取连接对象,并没有建立连接 HttpURLConnection conn = (Http ...
- java第二周周学习总结
java运算符和循环 java运算符 一.for 语句 for 语句的基本结构如下所示:for(初始化表达式;判断表达式;递增(递减)表达式){ 执行语句; //一段代码} 初始化表达式:初 ...
- my computer
把08年买的电脑换了,自己买的配件装的,狂拽酷炫叼炸天. 配置清单: CPU :英特尔至强E3-1230 V2 主板 :华硕P8Z77-V LX LX2 机箱 :撒哈拉海盗贼王Z6游戏机箱(白色限量版 ...
- eclipse中启动调试maven构建的javaweb项目
RT,遇到这个问题,我的第一反应就是找教程,今天折腾了一天,书也看了,博客也看了,maven视频都看了,有种做不出来的感觉了,带着前几个小时看的相关的资料和经验,就自己去摸索了. 直接开始说我是怎么做 ...
- (转载)哈夫曼编码(Huffman)
转载自:click here 1.哈夫曼编码的起源: 哈夫曼编码是 1952 年由 David A. Huffman 提出的一种无损数据压缩的编码算法.哈夫曼编码先统计出每种字母在字符串里出现的频率, ...
- dietpi请暂时不要升级为jessie
关于升级到Debian最新稳定版jessie,作者是这样说的:一旦官方Raspbian升级到Jessie,DietPi也会随之切换到Jessie.由于改动较大,很多功能需要修改后才能正常使用,到时候可 ...