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. windows使用sqlpus连接oracle 数据库

    1.先安装好Instant Client程序. 2.打开cmd 执行sqlplus命令,如果出现如下图 2.说明需要把oracle install 的bin目类 添加系统环境path中,如下图添加环境 ...

  2. 变量————if语句——结构使用

    1简述变量的命名规范 变量是以字母 数字 下划线组合而成 不能以数字开头 不能使用python中的关键字命名 变量要具有可描述性 区分大小写 name变量是什么数据类型通过代码检测 name = in ...

  3. Python笔记002-Python编程基础概念

    第二章(1):Python编程基础概念 1. Python 程序的构成 Python 程序有模块组成.一个模块对应 Python 源文件,一般后缀名是:.py. 模块有语句组成.运行 Python程序 ...

  4. java异常那些事

    异常的基本定义: 异常情形是指阻止当前方法或者作用域继续执行的问题.在这里一定要明确一点:异常代码某种程度的错误,尽管Java有异常处理机制,但是我们不能以“正常”的眼光来看待异常,异常处理机制的原因 ...

  5. BZOJ4199 NOI2015品酒大会(后缀树)

    利用SAM建出后缀树,树上每个节点计算一下|right|.right集合中ai的最大.次大.最小.次小值即可. #include<iostream> #include<cstdio& ...

  6. JS强制关闭浏览器页签并且不提示关闭信息

    工作中很多奇葩的需求都会出现,现在就有一个问题,描述如下: 现在的登录跳转权限页面要去掉,集成在第三方系统信息上,当退出登录的时候需要关掉打开的Tab页面,因此考虑使用window.close()关闭 ...

  7. sftp配置多个用户权限的问题

    groupadd group1 chmod 0755 /test/useradd -g group1 -d /test/backend/ -M test_backendusermod -s /sbin ...

  8. Jar包下载 开源网站 模板下载

    在日常的java学习和开发中,总是遇到各种jar包下载,但是CSDN这种坑爹网站,各位码农们都想挣点C币,一个开源的免费的东西就这么变了味,我这里收集 了一些好用的工具,日常开发中需要用的请自取,毕竟 ...

  9. Tomcat 和web 服务器配置

    mkdir /usr/local/tomcat # cd /usr/local/tomcat # tar -zxvf /software/apache-tomcat-7.0.54.tar.gz 生成链 ...

  10. XCode下在不同位置声明变量的用法(转)

    XCode下在不同位置声明变量的用法 方式一:直接在.h文件@interface中的大括号中声明. @interface Test : NSObject { NSString *str; // 私有变 ...