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. vs------密钥

    HM6NR-QXX7C-DFW2Y-8B82K-WTYJV

  2. Json数据可视化

    主要借助JSON.stringfy( value [, replacer] [, space] ). 一.参考文献 1.json数据可视化: http://www.cnblogs.com/lvdaba ...

  3. 使用Topshelf 开发windows服务

    在业务系统中,我们为了调度一些自动执行的任务或从队列中消费一些消息,所以基本上都会涉及到后台服务的开发.如果用windows service开发,非常不爽的一件事就是:调试相对麻烦,而且你还需要了解 ...

  4. JQuery.getJSON 从aspx页面返回JSON数据

    public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventAr ...

  5. Joomla![1.5-3.4.5]反序列化远程代码执行EXP(直接写shell)

    Usage:x.py http://xxx.com # coding=utf-8# author:KuuKi# Help: joomla 1.5-3.4.5 unserialize remote co ...

  6. 浅谈T-SQL中的子查询

    引言 这篇文章我们来简单的谈一下子查询的相关知识.子查询可以分为独立子查询和相关子查询.独立子查询不依赖于它所属的外部查询,而相关子查询则依赖于它所属的外部查询.子查询返回的值可以是标量(单值).多值 ...

  7. addEventListener和on的区别

    为什么需要addEventListener? 先来看一个片段: html代码 <div id="box">追梦子</div> 用on的代码 1 window ...

  8. html5 canvas(小树姐的牛掰到爆了的作品)

    自从小树嫁了个牛逼的前端之后,canvas的境界超过我了!!! 小树demo 小编表示:这个境界,这个几何,让我有种跪舔的感觉... http://www.wow-trend.com/brand/in ...

  9. vue-cli + webpack 多页面实例应用

    关于vue.js vue.js是一套构建用户界面的 轻型的渐进式前端框架.它的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.使用vue可以给你的开发带来极致的编程体验. 关于vu ...

  10. iOS原生的搜索:UISearchController

    iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...