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. 【转帖】史上最全PostgreSQL体系结构

    史上最全PostgreSQL体系结构 2019年07月16日 18:00:00 Enmotech 阅读数 35   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...

  2. java多线程上篇(一)

    操作系统与程序运行以及进程简介 一.线程与操作系统 操作系统是对计算机硬件资源的管理程序,是应用程序与计算机硬件交互的中间层,其本质仍旧是运行于硬件电路上的程序 对计算机硬件来说不存在操作系统,只是处 ...

  3. Python基础运算符(算数、比较、赋值、逻辑、成员)

    Python运算符有(算数运算符.比较运算符.赋值运算符.逻辑运算符.位运算符.成员运算符.身份运算符): 本程序包含算数.比较.赋值.逻辑.成员运算符. 1.运算符测试 #!/usr/bin/pyt ...

  4. 人机交互技术 Week 2_History of HCI

    Recap: Interaction Design Interaction Design Designing interactive products to support people in the ...

  5. PAT(B) 1006 换个格式输出整数(Java)

    题目链接:1006 换个格式输出整数 (15 point(s)) 代码 /** * Score 15 * Run Time 153ms * @author wowpH * @version 1.1 * ...

  6. json字符串,json对象,java对象互相转换

    1.把JSON字符串转换为JAVA 对象 JSONObject jsonobject = JSONObject.fromObject(jsonStr); User user= (User)JSONOb ...

  7. 关于mq的思考

    解耦场景 spark 发告警,同过kafka来解耦 削峰场景 日志采集生产环境几百台,当后续数量持续增加时,如果不加消息队列或者内存队列,可能把数据库打死 一个中间件: 为什么用 ->解决什么问 ...

  8. 【计算几何】Water Testing

    Water Testing 题目描述 You just bought a large piece of agricultural land, but you noticed that – accord ...

  9. Scratch技巧—-使用克隆技术实现菜单按钮

    昨天讲了克隆技术的一个具体应用:生成菜单按钮.有的小朋友迫不及待的试验了一下,发现菜单按钮是生成了,但是如何触发相应的按钮功能呢?触发功能的处理代码也是在克隆体里面实现哦.请看案例: 启动程序时,先隐 ...

  10. prometheus+grafana监控nginx

    被监控机器环境搭建&配置 nginx-module-vts下载: https://github.com/vozlt/nginx-module-vts nginx-module-vts安装 un ...