题目链接:https://leetcode-cn.com/problems/interleaving-string/description/

参考链接:https://blog.csdn.net/u011095253/article/details/9248073

     https://www.cnblogs.com/springfor/p/3896159.html

首先看到字符串的题目:  “When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”如果是子字符串和字符串匹配应该想到动态规划。

dp[i][j]表示s1取前i位,s2取前j位,是否能组成s3的前i+j位。

比如:s1="aabcc"  s2="dbbca" s3="aadbbcbcac"

dp的数组如上图所示。

dp[0][0]=1;

dp[0][1]:使用s1的第一个字符'1'可以组成s3的第一个字符。然后第二也是如此;aab!=aad。后面的也是一样。

对于dp[i][i]的状态显然是由两个方向的状态来决定的。dp[i][i]是由dp[i-1][j]和dp[i][j-1]来决定的。

public boolean isInterleave(String s1, String s2, String s3) {
if (s1 == null || s2 == null || s3 == null) return false;
if (s1.length() + s2.length() != s3.length()) return false;
int dp[][]=new int[s1.length() + 1][s2.length() + 1];
dp[0][0]=1;
for (int i = 1; i < dp.length; i++) {
if (s1.charAt(i-1)==s3.charAt(i-1)&&dp[i-1][0]==1) {
dp[i][0]=1;
}
}
for (int i = 1; i < dp[0].length; i++) {
if (s2.charAt(i-1)==s3.charAt(i-1)&&dp[0][i-1]==1) {
dp[0][i]=1;
}
}
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) { if (s1.charAt(i - 1) == s3.charAt(i + j - 1) && dp[i - 1][j]==1) {
dp[i][j] = 1;
}
if (s2.charAt(j - 1) == s3.charAt(i + j - 1) && dp[i][j - 1]==1) {
dp[i][j] = 1;
} }
}
return dp[dp.length-1][dp[0].length-1]==1;
}

动态规划之97 Interleaving String的更多相关文章

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

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

  2. 【LeetCode】97. Interleaving String

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

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

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

  4. [LeetCode] 97. Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...

  5. leetcode 97 Interleaving String ----- java

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

  6. 97. Interleaving String

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

  7. [leetcode]97. Interleaving String能否构成交错字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...

  8. 97. Interleaving String (String; DP)

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

  9. 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串

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

随机推荐

  1. 《大话设计模式》c++实现 装饰者模式

    一.UML图   介绍 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创 ...

  2. repr()函数

    http://www.cnblogs.com/itdyb/p/5046415.html

  3. 数据分析之Numpy库入门

    1.列表与数组 在python的基础语言部分,我们并没有介绍数组类型,但是像C.Java等语言都是有数组类型的,那python中的列表和数组有何区别呢? 一维数据:都表示一组数据的有序结构 区别: 列 ...

  4. UML之通信图

    通信图的概念:通信图(协作图)是表现对象交互关系的图,它展现了多个对象在协同工作达成共同目标的过程中互相通信的情况,通过对象和对象之间的链.发送的消息来显示参与交互的对象. 首先通信图一样是一种交互图 ...

  5. c++引用和指针的彻底理解

     ★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名.  ★ 区别: 1. 指针是一个实体,而引用仅是个别名: 2. 引用使用时无需解引用(*),指 ...

  6. vue-cli 脚手架搭建

    1,下载node.js node.js 集成npm 管理器 2,打开命令行工具(win+R) node -v npm -v 出现对应版本号,则安装完成 3,配置代理信息 详见代理设定:https:// ...

  7. qt5 移植 交叉编译出现错误

    类似这样的错误,当时没有完整的记下来,undefined reference to `std::__detail::_List_node_base@GLIBCXX_3.4.10 当时是在编译qt5cl ...

  8. Jersey入门——对Json的支持

    Jersey rest接口对POJO的支持如下: package com.coshaho.learn.jersey; import java.net.URI; import javax.ws.rs.C ...

  9. pyspider源码解读--调度器scheduler.py

    pyspider源码解读--调度器scheduler.py scheduler.py首先从pyspider的根目录下找到/pyspider/scheduler/scheduler.py其中定义了四个类 ...

  10. 几种线程安全的Map解析

    转载自 面试必问-几种线程安全的Map解析 HashMap线程安全的吗? Java中平时用的最多的Map集合就是HashMap了,它是线程不安全的. 看下面两个场景: 1.当用在方法内的局部变量时,局 ...