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. wifi display代码 分析

    转自:http://blog.csdn.net/lilian0118/article/details/23168531 这一章中我们来看Wifi Display连接过程的建立,包含P2P的部分和RTS ...

  2. 【JAVA集合框架之List与Set】

    一.概述 JAVA的集合框架中定义了一系列的类,这些类都是存储数据的容器.与数组.StringBuffer(StringBuilder)相比,它的特点是: 1.用于存储对象. 2.集合长度可变. 3. ...

  3. win7Java开发环境配置

    win7下Java开发环境的配置 首先下载符合操作系统版本的jdk,比如最新的jdk8: 下载链接:http://www.oracle.com/technetwork/java/javase/down ...

  4. HYSBZ 2440 完全平方数(莫比乌斯反演)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2440 若i为质数,n为i*i的倍数,则称n为含平方因子数. 求1~n的无平方因子数. F(x) ...

  5. android 面试题

    一,什么是OOM (1)先从定义开始:Android(Java)中常见的容易引起内存泄漏的不良代码Android主要应用在嵌入式设备当中,而嵌入式设备由于一些众所周知的条件限制,通常都不会有很高的配置 ...

  6. C#交互功能的演化

    (此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:Miguel de Icaza在最近发表的一篇博文中畅谈了Mono及其相关产品中的C#交互特性的演化情况. ...

  7. 在Visualforce page中用自带的控件实现Ajax回调后台方法(并且可以用js去动态给parameters赋值)

    这里用的组合是:apex:commandLink  + apex:actionFunction + apex:outputPanel 这里的 apex:commandLink 和 apex:actio ...

  8. 用Access作为后台数据库支撑。

    /// <summary> /// 读取Excel文档 /// </summary> /// <param name="Path">文件名称&l ...

  9. 自定义ActionBar

    /** * 1.创建ActionBar对象getSupportActionBar() * 2.布置自己的ActionBar布局(在res/layout) * 3.把自定义的ActionBar布局加载到 ...

  10. 关闭Android/iPhone浏览器自动识别数字为电话号码

    <meta name="format-detection" content="telephone=no"><meta http-equiv=& ...