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.

定义boolean 数组result[][]表示s1的前i个字符和s2的前j个字符是否能交替组成s3的前i+j个字符。

function:result[i][j] =   result[i-1][j] &&(s1[i-1] == s3[i+j-1]) ||  result[i][j -1] &&(s2[j-1] == s3[i+j-1])

initialize:

result[i][0] = (s1[0...i-1] == s3[0...i-1])

result[0][j] = (s2[0...i-1] == s3[0...i-1])

返回值: result[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;
}
int m = s1.length();
int n = s2.length();
boolean[][] result = new boolean[m + 1][n + 1];
result[0][0] = true; for (int i = 1; i < m + 1; i++) {
if (result[i - 1][0] && s1.charAt(i - 1) == s3.charAt(i - 1)){
result[i][0] = true;
}
}
for (int j = 1; j < n + 1; j++) {
if (result[0][j - 1] && s2.charAt(j - 1) == s3.charAt(j -1)){
result[0][j] = true;
}
}
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (result[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1) || result[i][j - 1] && s2.charAt(j -1 ) == s3.charAt(i + j - 1)){
result[i][j] = true;
}
}
}
return result[m][n];
}
}

Interleaving String leetcode的更多相关文章

  1. Interleaving String leetcode java

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

  2. Interleaving String——Leetcode

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

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

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

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

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

  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

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

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

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

  8. 【LeetCode】97. Interleaving String

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

  9. 40. Interleaving String

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

随机推荐

  1. linux中权限的修改

    修改访问权限的linux名是:Linux访问权限的问题是这样子的:比如 d rwx rwx rwx ,d是文件所在的文件,后面有9位,分别代表不同者的权限.第一个rwx代表这文件的所有者的权限,r是r ...

  2. nginx的在linux系统中的安装

    1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. n  gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果 ...

  3. python学习笔记-(十)面向对象基础

    面向对象相关知识简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...

  4. WinForm------窗体初始化位置的显示

    在窗体的构造方法里面添加 public Form2() { InitializeComponent(); //指定窗口初始化时的位置(计算机屏幕中间) this.StartPosition = For ...

  5. JavaScript学习笔记——对象分类

    对象的分类 一.对象的分类 1.内置对象 Global Math 2.本地对象 Array Number String Boolean Function RegExp 3.宿主对象 DOM BOM 二 ...

  6. Java web 使用页面压缩

    借助类,相关依赖: <!-- https://mvnrepository.com/artifact/net.sourceforge.pjl-comp-filter/pjl-comp-filter ...

  7. Winsock 入门 Echo 示例

    #include <stdio.h> #include <winsock2.h> #pragma comment(lib, "ws2_32") /* Win ...

  8. ecshop团购显示“库存不足”

    产生原因:是因为产品设置了多属性 解决办法:打开group_buy.php 第 267行找到 empty($product_info) ? $product_info = array(, ) : '' ...

  9. php构造函数连接数据库

    index.php require_once("mysql.config.php"); require_once("mysql.class.php"); ech ...

  10. 计算div里面li个数

    方式一:js var content=document.getElementById("content"); alert(document.getElementsByTagName ...