Given s1s2s3, 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.

题目大意:给定三个字符串,s1,s2,s3,问s3是否由s1和s2交叉构成。

解题思路:递归超时,重复计算太多,加上cache可能可以。

说一下dp的方法,研究了半天才看明白,用一个二维布尔数组dp[i][j]表示从s1中取前i位,从s2中取前j位构成s3的前i+j位是否可以。

那么dp[i][j]可能从dp[i-1][j]或dp[i][j-1]而来:

  如果从dp[i-1][j]来,那么dp[i-1][j]应该为true,并且s1.charAt(i-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;

  如果从dp[i][j-1]来,那么dp[i][j-1]应该为true,并且s2.charAt(j-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;

初始化,dp[0][0]=true表示两个空字符串构成一个空字符串为true。

另外,从s1或s2开始构成s3的前n或m个字符也是初始条件。

举个列子,注意左上角那一对箭头指向的格子dp[1][1], 表示s1取第1位a, s2取第1位d,是否能组成s3的前两位aa

从dp[0][1] 往下的箭头表示,s1目前取了0位,s2目前取了1位,我们添加s1的第1位,看看它是不是等于s3的第2位,( i + j 位)

从dp[1][0] 往右的箭头表示,s1目前取了1位,s2目前取了0位,我们添加s2的第1位,看看它是不是等于s3的第2位,( i + j 位)

  public static boolean isInterleave(String s1, String s2, String s3) {
if (s3.length() != s1.length() + s2.length()) {
return false;
}
boolean[][] dp = new boolean[s1.length() + 1][s2.length() + 1];
dp[0][0] = true;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == s3.charAt(i)) {
dp[i + 1][0] = dp[i][0];
}
}
for (int i = 0; i < s2.length(); i++) {
if (s2.charAt(i) == s3.charAt(i)) {
dp[0][i + 1] = dp[0][i];
}
}
for (int i = 1; i < s1.length() + 1; i++) {
for (int j = 1; j < s2.length() + 1; j++) {
dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
return dp[s1.length()][s2.length()];
}

参考:http://blog.csdn.net/u011095253/article/details/9248073

Interleaving String——Leetcode的更多相关文章

  1. Interleaving String leetcode

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

  2. Interleaving String leetcode java

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

  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. spring Aop 注解

    个人理解: spring Aop 是什么:面向切面编程,类似于自定义拦截操作,支持拦截之前操作@Before,拦截之后操作@After,拦截环绕操作@Around. 什么情况下使用spring Aop ...

  2. 10_Mybatis开发Dao方法——mapper代理实现

    [工程截图(几个关键的标红框)] [UserMapper.xml] <?xml version="1.0" encoding="UTF-8"?> & ...

  3. C/C++中memset函数

    本文学习参考http://baike.baidu.com/link?url=ZmSyY8ciB_nJt9KM-W2fiEFJrC2mugFsLqRdY2b4pLe8rD_jRXyN7_pj0GBBD2 ...

  4. GitHub Windows客户端部署

    下载网址:https://windows.github.com 点击下载按钮即可下载一个小程序,这个小程序会去服务器端下载完整的Windows版客户端,然后自动安装. 如果安装出错的话,那就打开IE浏 ...

  5. MongoDB源码分析——mongod数据查询操作

    源码版本为MongoDB 2.6分支 Edit mongod数据查询操作 在mongod的初始化过程中说过,服务端接收到客户端消息后调用MyMessageHandler::process函数处理消息. ...

  6. LAMP 环境 快速安装

    (一)安装Apache 1.下载安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 yum install zlib-devel -y wget http://m ...

  7. WinForm TreeView 三种状态

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { var node = e.N ...

  8. less学习-语法(二)

    变量 @color1:#fff; 选择器  // Variables @mySelector: banner; // Usage .@{mySelector} { font-weight: bold; ...

  9. MySQL - “Timeout error occurred trying to start MySQL Daemon”解决方法

    前几天,网站出现Many connections的问题,进入mysql,show full processlist发现有154个进程,晕....直接service mysqld restart 但是不 ...

  10. python开发-web框架之diango-----Models

    这篇博客是紧连上一篇博客的,因为内容较多,这里介绍的是Models这一部分的内容 七:Models 数据库的配置 1    django默认支持sqlite,mysql, oracle,postgre ...