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.

Solution 1: 指针的做法。。。错误!

比如例子: 在这种情况下,既可以进入s1,又可以进入s2,到底该进哪个呢,不能定。

Input: "aa", "ab", "abaa"
Output: false
Expected: true
 public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if(s1==null||s1.length()==0)
return s2.equals(s3);
if(s2==null||s2.length()==0)
return s1.equals(s3);
int l1=s1.length();
int l2=s2.length();
int l3=s3.length();
if(l3!=(l1+l2))
return false;
int index1=0;
int index2=0;
int index3=0;
while(index1<l1&&index2<l2){
if(s1.charAt(index1)==s3.charAt(index3)){
index1++;
index3++;
}else if(s2.charAt(index2)==s3.charAt(index3)){
index2++;
index3++;
}else{
return false;
}
}
return true;
}
}

Solution 2: DP.

http://www.cnblogs.com/springfor/p/3896159.html

“When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”

所以这道题还是用DP的思想解决。

大体思路是,s1取一部分s2取一部分,最后是否能匹配s3。

动态规划数组是dp[i][j],表示:s1取前i位,s2取前j位,是否能组成s3的前i+j位。

初始化是,假设s1为空,那么s2每一位跟s3匹配放入dp[0][j];假设s2为空,那么s1每一位跟s3匹配放入dp[i][0]。

下面就继续匹配。讲解引用自: http://blog.csdn.net/u011095253/article/details/9248073

那什么时候取True,什么时候取False呢?

False很直观,如果不等就是False了嘛。

那True呢?首先第一个条件,新添加的字符,要等于s3里面对应的位( i + j 位),第二个条件,之前那个格子也要等于True

举个简单的例子s1 = ab, s2 = c, s3 = bbc ,假设s1已经取了2位,c还没取,此时是False(ab!=bb),我们取s2的新的一位c,即便和s3中的c相等,但是之前是False,所以这一位也是False

同理,如果s1 = ab, s2 = c, s3=abc ,同样的假设,s1取了2位,c还没取,此时是True(ab==ab),我们取s2的新的一位c,和s3中的c相等,且之前这一位就是True,此时我们可以放心置True (abc==abc)

 public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1 == null || s1.length() == 0)
return s2.equals(s3);
if (s2 == null || s2.length() == 0)
return s1.equals(s3);
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
if (l3 != (l1 + l2))
return false;
boolean[][] state = new boolean[l1 + 1][l2 + 1];
state[0][0]=true;
for(int i=1;i<=l1;++i){
state[i][0]=state[i-1][0]&&(s1.charAt(i-1)==s3.charAt(i-1));
}
for(int i=1;i<=l2;++i){
state[0][i]=state[0][i-1]&&(s2.charAt(i-1)==s3.charAt(i-1));
}
for(int i=1;i<=l1;++i){
for(int j=1;j<=l2;++j){
state[i][j]=(state[i][j-1]&&(s2.charAt(j-1)==s3.charAt(i+j-1)))||(state[i-1][j]&&(s1.charAt(i-1)==s3.charAt(i+j-1)));
}
}
return state[l1][l2];
}
}

[Leetcode] Interleaving String的更多相关文章

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

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

  2. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

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

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

  4. [LeetCode] Interleaving String 解题思路

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

  5. [LeetCode] Interleaving String [30]

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

  6. [leetcode]Interleaving String @ Python

    原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...

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

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

  8. 【leetcode】Interleaving String

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

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

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

随机推荐

  1. 深入理解SELinux SEAndroid

    参考文章: 一. http://blog.csdn.net/innost/article/details/19299937 二. http://blog.csdn.net/innost/article ...

  2. 在ubuntu上搭建开发环境7---ubuntu安装JDK

    首先,当然是要下载了. 地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 按 ...

  3. Pyqt QComboBox 省市区县联动效果

    在Qt中, QComboBox方法窗口组件允许用户从列表清单中选择,在web中就是select标签,下拉选项. 省市区县的联动就是currentIndexChanged 获取当前的Index,通过这个 ...

  4. [LeetCode] Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  5. GMap.Net开发之在地图上添加多边形

    上一篇介绍了在GMap上添加自定义标签(GMapMarker),这篇介绍在GMap上添加多边形(GMapPolyogn),并且介绍如何在地图上画任意的多边形. 如果已经知道了多边形的各个点的位置,就可 ...

  6. 攻城狮在路上(叁)Linux(二十三)--- linux磁盘参数修改(设备代码、设备名)

    一.mknod:设置设备代码 linux中,所有的设备都是用文件来表示,文件通过major与minor数值来判断. major为主设备代码,minor为设备代码(需要查询),示例如下: /dev/hd ...

  7. 攻城狮在路上(壹) Hibernate(八)--- 映射Hibernate组成关系

    一.使用组成关系的原则: 在不导致数据冗余的前提下,尽可能减少数据库表的数目及表之间的外键参照关系,因为建立多个表的连接是很耗时的操作. 举例说明:Customer类中的Address属性,可以通过组 ...

  8. [Surface] 在win8.1上使用QQ截图放大问题(解决办法)

    在使用每次截图的时候整个都被放大了,很让人郁闷,截不到完整的图,本着遇到问题解决问题的想法,这事早解决早好.   开工: 1. 度娘上搜索"win8 qq截图 放大",找到很多资料 ...

  9. 【使用Unity开发Windows Phone上的2D游戏】(1)千里之行始于足下

    写在前面的 其实这个名字起得不太欠当,Unity本身是很强大的工具,可以部署到很多个平台,而不仅仅是可以开发Windows Phone上的游戏. 只不过本人是Windows Phone 应用开发出身, ...

  10. uri,url.urn

    uri:Web上可用的每种资源 - HTML文档.图像.视频片段.程序等 - 由一个通过通用资源标志符(Universal Resource Identifier, 简称"URI" ...