097 Interleaving String 交错字符串
给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的。
例如,
给定:
s1 = "aabcc",
s2 = "dbbca",
当 s3 = "aadbbcbcac", 返回 true.
当 s3 = "aadbbbaccc", 返回 false.
详见:https://leetcode.com/problems/interleaving-string/description/
Ø d b b c a
Ø T F F F F F
a T F F F F F
a T T T T T F
b F T T F T F
c F F T T T T
c F F F T F T
字符串s1和s2的长度和必须等于s3的长度,如果不等于,肯定返回false。那么当s1和s2是空串的时候,s3必然是空串,则返回true。所以直接给dp[0][0]赋值true,然后若s1和s2其中的一个为空串的话,那么另一个肯定和s3的长度相等,则按位比较,若相同且上一个位置为True,赋True,其余情况都赋False,这样的二维数组dp的边缘就初始化好了。在任意非边缘位置dp[i][j]时,它的左边或上边有可能为True或是False,两边都可以更新过来,只要有一条路通着,那么这个点就可以为True。那么分别来看,如果左边的为True,那么去除当前对应的s2中的字符串s2[j - 1] 和 s3中对应的位置的字符相比(计算对应位置时还要考虑已匹配的s1中的字符),为s3[j - 1 + i], 如果相等,则赋True,反之赋False。 而上边为True的情况也类似,所以可以求出递推公式为:
dp[i][j] = (dp[i - 1][j] && s1[i - 1] == s3[i - 1 + j]) || (dp[i][j - 1] && s2[j - 1] == s3[j - 1 + i]);
其中dp[i][j] 表示的是 s2 的前 i 个字符和 s1 的前 j 个字符是否匹配 s3 的前 i+j 个字符。
Java实现:
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
int m=s1.length();
int n=s2.length();
if(m+n!=s3.length()){
return false;
}
boolean[][] path=new boolean[m+1][n+1];
for(int i=0;i<m+1;++i){
for(int j=0;j<n+1;++j){
if(i==0&&j==0){
path[i][j]=true;
}else if(i==0){
path[i][j]=path[i][j-1]&&(s2.charAt(j-1)==s3.charAt(j-1));
}else if(j==0){
path[i][j]=path[i-1][j]&&(s1.charAt(i-1)==s3.charAt(i-1));
}else{
path[i][j] = (path[i][j-1] && (s2.charAt(j-1)==s3.charAt(i+j-1)))
|| (path[i-1][j] && (s1.charAt(i-1)==s3.charAt(i+j-1)));
}
}
}
return path[m][n];
}
}
参考:https://www.cnblogs.com/grandyang/p/4298664.html
097 Interleaving String 交错字符串的更多相关文章
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
- 97. Interleaving String(字符串的交替连接 动态规划)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- Java for LeetCode 097 Interleaving String 【HARD】
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [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】#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 ...
随机推荐
- H5与native有啥区别?
app测试,H5与native有啥区别? native是使用原生系统内核的,相当于直接在系统上操作.是我们传统意义上的软件,更加稳定. 但是H5的APP先得调用系统的浏览器内核,相当于是在网页中进行操 ...
- c++变量定义
float **a 表示a是一个“指针的指针”,也可以理解为是一个二维数组的指针,***a具有类似的解释,可以理解为是一个三维数组的指针.
- the art of seo(chapter six)
Developing an SEO-Friendly Website ***Making Your Site Accessible to Search Engines***1.Indexable Co ...
- hdu 1205 吃糖果(抽屉原理)
题意:N种糖果,不能把一样的放在一起吃,即第一次吃一种,下一次吃另一种. 思路:找到个数最多的糖果种类(最大的数目记作 ma,该糖果种类记为a),首先把这n个糖果放到n个抽屉里面,然后把剩余的N-1种 ...
- Web app root system property already set to different value: 'webapp.root'
java.lang.IllegalStateException: Web app root system property already set to different value: 'webap ...
- css中块元素和行内元素区别
行内元素特点 1.和其他元素都在一行上: 2.元素的高度.宽度.行高及顶部和底部边距不可设置: 3.元素的宽度就是它包含的文字或图片的宽度,不可改变. 块元素特点 1.每个块级元素都从新的一行开始,并 ...
- leetcode398 and leetcode 382 蓄水池抽样算法
382. 链表随机节点 给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样. 进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? 示 ...
- CF-798A
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- ORM框架SQLAlchemy学习
一.基本介绍 以下介绍来自维基百科,自由的百科全书. SQLAlchemy是Python编程语言下的一款开源软件.提供了SQL工具包及对象关系映射(ORM)工具,使用MIT许可证发行. SQLAlch ...
- ElasticSearch基础之批量操作(mget+mbulk)
在前面的演示中,我们都是基于一次http查询,每次查询都要建立http的三次握手请求,这样比较耗费性能!因此ES给我们提供了基本的批量查询功能,例如如下的查询,注意里面的index是可以任意指明的 ...