题目

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.

题解

这道题还是像之前我引过的那句话:

“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)

代码如下:

 1     public boolean isInterleave(String s1, String s2, String s3) {
 2        if(s3.length()!=s1.length()+s2.length()) 
 3        return false;    
 4         
 5        boolean [][] dp = new boolean [s1.length()+1][s2.length()+1];
 6        dp[0][0]=true;
 7        
 8        for(int i = 1; i<=s1.length() && s1.charAt(i-1)==s3.charAt(i-1); i++)
 9            dp[i][0]=true;
        
        for(int i = 1; i<=s2.length() && s2.charAt(i-1)==s3.charAt(i-1); i++)
            dp[0][i]=true;
        
        for(int i = 1; i <= s1.length(); i++){
            for(int j = 1; j <= s2.length(); j++){
                char c = s3.charAt(i+j-1);
                if(c == s1.charAt(i-1) && dp[i-1][j])
                  dp[i][j] = true;
                  
                if(c == s2.charAt(j-1) && dp[i][j-1])
                  dp[i][j] = true;
            }
        }
       return dp[s1.length()][s2.length()];
     }

Reference:

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

http://www.cnblogs.com/lichen782/p/leetcode_interleaving_string.html

Interleaving String leetcode java的更多相关文章

  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

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

  3. Scramble String leetcode java

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  4. Reverse Words in a String leetcode java

    题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...

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

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

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

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

  7. Leetcode:Interleaving String 解题报告

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

  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. 暑假热身 D. 条形码设计

    校ACM队准备筹划向学校批请一个专用机房.但是为了防止它变成公用机房,FL建议采用刷卡进入的办法,她设计了一种条形码,每人都对应一个.这种大小为2*n的条形码由以下三种元素构成:1*2.2*1.2*2 ...

  2. MySQL 索引详解大全

    什么是索引? 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中. 2. 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结 ...

  3. C 结构体小结

    看了三天结构体,是时候总结一下了. 关于结构体的声明: struct Student { ]; char sex; int age; ]; }; /*然后定义一个Student 类型的 student ...

  4. 查看现有运行的linux服务器有多少内存条

    i161 admin # ssh 192.168.5.209 dmidecode | grep 'Ending Address' -B1 -A2    Starting Address: 0x0000 ...

  5. IOC原理解释

    spring ioc它其实是一种降低对象耦合关系的设计思想,通常来说,我们在一个类调用另一个类的方法的时候,需要不断的new新的对象来调用该方法,类与类之间耦合度比较高,有了ioc容器以后,ico容器 ...

  6. STM32canopen调试

    问题1:用usbcan监测不到can口的报文 属于接线问题 CANopen程序总使用的是can1 对应的接下口在J1的1和2口,而其接口排序是从外向里排序,故最外面的为1号接口,由于接线时,按照左边的 ...

  7. There is no getter for property named 'purchaseApplyId' in 'class java.lang.Long'

    mapper.xml: <delete id="deleteByPurchaseAppyId" parameterType="Long"> < ...

  8. mysql lower,upper实现大小写

    mysql的lower和uppper函数可以将指定字符串转换为小写和大写 select lower('OutSpringTd') as lowerCase, upper('OutSpringTd') ...

  9. iOS 端的第三方语音识别库

    最近在看语音识别方面的库,主要看了2个收费的项目,一个是 At&t 的,一个是Nuance的.这2个项目虽然是收费的,但是仅仅测试的话,是免费的,连接如下 https://developer. ...

  10. Java用Scanner类获取用户输入

    用Java编写程序时,有些数据需要用户输入,这个时候需要调用java提供的Scanner类,这个类在包java.util下,比如求一个矩形的面积,简单的看一下用法: import java.util. ...