题目

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. MySQL建立索引的注意事项

    对于大数据量的表格,尤其是百万行以上的数据表,一定要对其建立索引,否则查询速度极慢.(参考后面的测试结果)建立索引时需注意: MySQL的索引有两种:单列索引(即在某一列上建索引).多列组合索引(即在 ...

  2. 90天打造日均在线网站1W+的友情链接平台

    导读:三个月过去了,好友张森终于把一款默默无名的软件打造出了日均1W+在线的平台,我认为成功的因素很简单,1,找准了用户群体的痛点;2,肯花精力做运营;3,合理的推广.本文是他的自述,打造一款产品,说 ...

  3. recv和send函数

    转自  http://www.cnblogs.com/blankqdb/archive/2012/08/30/2663859.html 1. send解析 sockfd:指定发送端套接字描述符. bu ...

  4. ZOJ 2315

    ---恢复内容开始--- http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 这个题目比较难以看懂,我也是看网上的题目意思才 ...

  5. 39.递归颠倒栈[ReverseStack]

    [题目] 用递归颠倒一个栈.例如输入栈{1, 2, 3, 4, 5},1在栈顶.颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶. [分析] 乍一看到这道题目,第一反应是把栈里的所有元素逐一p ...

  6. 4.python函数基础

    一.函数 1.函数简介 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但 ...

  7. jquery去掉或者替换字符,设置指定options为selected状态

    <html> <body> <div><select id="queryYear">                 <opt ...

  8. ios学习总结(2) -- UIButton的使用

    原文地址 UIButton的类是一个UIControl子类,它实现了在触摸屏上的按钮.触摸一个按钮拦截事件和动作消息发送到目标对象时,它的挖掘.设定的目标和行动方法都继承自UIControl.这个类提 ...

  9. July 26th, Week 31st Tuesday, 2016

    The best preparation for tomorrow is doing your best today. 对明天最好的准备就是今天做到最好. The road toward tomorr ...

  10. 【读书笔记】读《JavaScript高级程序设计-第2版》 - 非函数部分

    章节列表: 第08章:BOM 第09章:客户端检测 第10章:DOM 第11章:DOM2和DOM3 第12章:事件 第13章:表单脚本 第14章:错误处理与调试 第17章:Ajax和JSON第20章: ...