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. [转帖]8086 CPU 寄存器简介

    8086 CPU 寄存器简介 https://www.cnblogs.com/BoyXiao/archive/2010/11/20/1882716.html 哎 没看完 感觉好复杂. 引子 打算写几篇 ...

  2. python读写文件的操作

    编程语言中,我们经常会和文件和文件夹打交道,这篇文章主要讲的是Python中,读写文件的常用操作: 一.打开文件 openFile = open('../Files/exampleFile.txt', ...

  3. maven 依赖 无法下载到jar包,典型的json-lib包

    <dependency>            <groupId>net.sf.json-lib</groupId>            <artifact ...

  4. c语言 c++程序运行过程

    我们写好的  .c  .cpp   文件在计算机中如何运行. 一个.c 文件  .cpp  文件  首先经过 预编译形成  . i 文件  在这个过程中 主要处理程序中的#  以及进行宏替换 然后编译 ...

  5. java. util. concurrent. atomic

    一.原子更新基本类型 AtomicInteger AtomicBoolean AtomicLong 二.原子更新数组 AtomicIntegerArray AtomicLongArray Atomic ...

  6. Codeforces Round #557 Div. 1 based on Forethought Future Cup - Final Round

    A:开场就读错题.读对了之后也没啥好说的. #include<bits/stdc++.h> using namespace std; #define ll long long #defin ...

  7. java 框架-模板引擎FreeMarker

    https://www.cnblogs.com/itdragon/p/7750903.html FreeMarker是一个很值得去学习的模版引擎.它是基于模板文件生成其他文本的通用工具.本章内容通过如 ...

  8. siamese跟踪论文思考

    转载自:https://zhuanlan.zhihu.com/p/34222060 通过作者在专栏里面放的几张响应图我们可以看到:SiamFC并不能区分不同的物体,图片上所有具有语义信息的物体都会得到 ...

  9. 2、Java基础:概念

    1.面向对象和面向过程的区别 面向过程 优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机.嵌入式开发.Linux/Unix等一般采用面向过程开发,性能是最重要的因素 ...

  10. Java中的ThreadLocal详解

    一.ThreadLocal简介 多线程访问同一个共享变量的时候容易出现并发问题,特别是多个线程对一个变量进行写入的时候,为了保证线程安全,一般使用者在访问共享变量的时候需要进行额外的同步措施才能保证线 ...