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.

State:f[i][j]  表示s1的前i 个字符 和 s2的前j 个字符能组成s3的前 i + j 个字符

Function: if (((s1.charAt(i - 1) == s3.charAt(i + j - 1) && interleave[i - 1][j])) || (s2.charAt(j - 1) == s3.charAt(i + j - 1) && interleave[i][j - 1])) {
                    interleave[i][j] = true;

Initializtion:f[0][0] = true    f[i][0]  i = (1 ~ s1.length() )  f[0][j]  j = (1 ~ s2.length())

Answer:f[s1.length()][s2.length()]

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

Interleaving String (DP)的更多相关文章

  1. 40. Interleaving String

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

  2. 【leetcode】Interleaving String

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

  3. 二维动态规划——Interleaving String

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

  4. LeetCode-Interleaving String[dp]

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

  5. Leetcode:Interleaving String 解题报告

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

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

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

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

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

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

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

  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. ${__setProperty 等常见jmeter参数相关博客汇总

    jmeter 控制线程组执行顺序   这个要配合全局变量.if和while来实现BeanShell取样器,全局变量:${__setProperty(newswitch,${switch1},)}if条 ...

  2. 【ZOJ】4012 Your Bridge is under Attack

    [ZOJ]4012 Your Bridge is under Attack 平面上随机n个点,然后给出m条直线,问直线上有几个点 \(n,m \leq 10^{5}\) 由于共线的点不会太多,于是我们 ...

  3. Netty框架原理

    用这张图表示的就是一个基本的Netty框架 通过创建两个线程池,一个负责接入, 一个负责处理 public class Start { public static void main(String[] ...

  4. Python列表排序方法reverse、sort、sorted详解

    python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...

  5. STM32与ARM代码执行过程

    内存分配 1.ARM(JZ2440) 启动方式: 1)nor启动 注:1.bootloader烧在norflash的0地址 2.将bootloader从norflash中复制到SDRAM中的链接地址( ...

  6. c语言 运算器

    #include<stdio.h> #include<stdbool.h> void menu(){ printf("************************ ...

  7. C语言之反汇编揭秘

    title: 'C语言之反汇编揭秘' tags: 汇编与反汇编 categories: 汇编与反汇编 copyright: true abbrlink: 'b1c9' date: 2019-09-07 ...

  8. (二)CXF之用CXF官方工具生成客户端Client

    一.CXF工具的下载与使用 登录CXF官网:http://cxf.apache.org/download.html 下载,本系列使用的是3.1.5版本: 添加path环境变量 二.案例 2.1 发布w ...

  9. 奇妙的算法【10】TX--有效号码、最,小耗时、最小差值、差值输出、异或结果

    昨晚刚刚写的几道算法题,难度也还行,就是全部AC有些困难,当时第一题AC.第二题AC 60%,第四题AC 40%,第五题没有时间写完了,这个应该全部AC了:其中第三题没有写出来 1,是否存在符合规范的 ...

  10. TSec《mysql client attack chain》

    从这个议题学到挺多,攻击手法的串联. 1.mysql Client Attack 这个攻击手法去年就爆出来了,本质就是mysql协议问题,在5步篡改读取客户端内容,导致任意文件读取,如下图所示. 修改 ...