给定三个字符串 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 交错字符串的更多相关文章

  1. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

  2. 97. Interleaving String(字符串的交替连接 动态规划)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  3. 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 ...

  4. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  5. 40. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  6. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  7. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  8. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  9. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

随机推荐

  1. iOS:UITextField中文输入法输入时对字符长度的限制

      如题的问题,又是个让我抓狂了大半天的问题,还是做个记录,有与类似问题的同学可参考,但不一定对.具体问题还需具体分析.我遇到的需求是这样的:有一个输入框,输入框内输入文字,文字字数限制在20字.   ...

  2. 客户端调用cxf发布的服务

    import java.util.ArrayList; import java.util.List; import javax.xml.namespace.QName; import org.apac ...

  3. 【转】澄清P问题、NP问题、NPC问题

    首先,原文链接.(这篇文章让我第一次有了感谢腾讯,感谢微信,感谢微信公众号的冲动.总之,非常感谢作者的分享.) 然后:结论图如下 担心万一哪天原网站把这篇文章下线,所以原文内容复制过来. 澄清P问题. ...

  4. Spring cloud基础模块学习

    1.微服务架构构成 单体架构(通过应用集群和数据库集群来提高性能,多余模块存在浪费) 垂直架构(新的功能模块通过新项目来实现,数据库之间存在交叉关联.存在数据冗余,和单体架构一样通过扩展集群结点,成本 ...

  5. 初始化cache_dir(squid)

    sed -i '/adjustFactor/d' /CNCLog/exactTraffic/conf/localTraffic.cfgecho "adjustFactor = '-0.67 ...

  6. BZOJ_2438_[中山市选2011]杀人游戏 _强连通分量

    BZOJ_2438_[中山市选2011]杀人游戏 _强连通分量 Description 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手.警察能够对每一个人 ...

  7. 第一行代码笔记的思维导图(http://images2015.cnblogs.com/blog/1089110/201704/1089110-20170413160323298-819915364.png)

  8. libvirt监控

  9. 20个Flutter实例视频教程-第06节: 酷炫的路由动画-2

    博客地址: https://jspang.com/post/flutterDemo.html#toc-94f 视频地址: https://jspang.com/post/flutterDemo.htm ...

  10. Flutter实战视频-移动电商-53.购物车_商品列表UI框架布局

    53.购物车_商品列表UI框架布局 cart_page.dart 清空原来写的持久化的代码; 添加对应的引用,stless生成一个静态的类.建议始终静态的类,防止重复渲染 纠正个错误,上图的CartP ...