给定三个字符串 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. ReentrantReadWriteLock读写锁实现分析

    排他锁在同一时刻只允许一个线程进行访问,而读写锁在同一时刻允许多个读线程访问,但是在写线程访问时,所有的读线程和其他的写线程均被阻塞.读写锁内部维护了一对锁,一个读锁和一个写锁,通过分离读锁和写锁,使 ...

  2. iOS 两个tableview的 瀑布流

    iOS 两个tableview的 瀑布流1. [代码]Objective-C     ////  DocViewController.m//  getrightbutton////  Created ...

  3. TP框架中的多种方法代码(C,G,L,T,I,N,D,M,A,R,B,U,W,S,F,E)

    C方法 function C($name=null, $value=null,$default=null) { static $_config = array(); // 无参数时获取所有 if (e ...

  4. Python: PS 滤镜--旋转模糊

    本文用 Python 实现 PS 滤镜中的旋转模糊,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/392 ...

  5. 日志的打印 —— Java 支持

    1. java.util.logging.Logger 日志级别(logLevel) OFF,Integer.MAX_VALUE SEVERE,1000 WARNING,900 INFO,800 CO ...

  6. 关于try catch finally的执行顺序解释

    偶然遇到了被问到finally的执行问题,忽然发现一直用的都是try catch 没有用过finally的情况,所以目前总结一下. 先抛出结论: 1.try内部正常执行try的内部逻辑,异常则执行ca ...

  7. 【转】Oracle Freelist和HWM原理及性能优化

    文章转自:http://www.wzsky.net/html/Program/DataBase/74799.html 近期来,FreeList的重要作用逐渐为Oracle DBA所认识,网上也出现一些 ...

  8. 如何正确遍历删除List中的元素,你会吗?

    遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合 ...

  9. 理解复杂的const和typedef和指针的关系

    // container.cpp : 定义控制台应用程序的入口点. //   #include "stdafx.h" #include<iostream> #inclu ...

  10. Understand中的Graphical Views使用

    Graphical Views 用于浏览代码结构. 下面以dso为例 1.Butterfly 显示include关系.例: 2.Declaration 文件中的类.例: 3.UML Class Dia ...