[leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
题意:
给定s1和s2,判断给定的s3是不是s1和s2交织相错后可以生成的字符串

思路:
遇到字符串的子序列或匹配问题巴普洛夫狗流哈喇子实验般的想到dp
s1 = 0 "a a b c c",
0 T
s2 = "d
b
b
c
a",
------------------------ s3 = "aadbbcbcac"
初始化:
dp[0][0] = true
考虑是否需要预处理第一个row: dp[0][j] ? 需要!处理极端情况即s3的字符完全来自s1,则if s1.charAt(j-1) == s3.charAt(j-1) , dp[0][j] = dp[0][j-1]
考虑是否需要预处理第一个col : dp[i][0] ? 需要!处理极端情况即s3的字符完全来自s2,则if s2.charAt(i-1) == s3.charAt(i-1) , dp[i][0] = dp[i-1][0]
对于dp[i][j]
s3下一个字符,要么来自s1,要么来自s2
dp[i][j] = (dp [i-1][j] && s2.charAt(i-1) == s3.charAt(i + j -1) )
||(dp [i][j-1] && s1.charAt(j-1) == s3.charAt(i + j -1) );
【注意,之前错写成】
if s2.charAt(i-1) == s3.charAt(i + j -1),  dp [i][j] = dp [i-1][j]
if s1.charAt(j-1) == s3.charAt(i + j -1),   dp [i][j] = dp [i][j-1]
为何错? 因为dp[i][j] 若此时等于'b' 而此时s1有'b' , s2有'b', dp[i][j] 就会两个if语句都进入,最终被先后赋值两次。
代码:
 class Solution {
     public boolean isInterleave(String s1, String s2, String s3) {
         if(s1.length() + s2.length() != s3.length()) return false;
         boolean[][] dp = new boolean[s2.length() + 1][s1.length() + 1];
         // init
         dp[0][0] = true;
         for(int i = 1; i<= s2.length(); i++){
             if(s2.charAt(i-1) == s3.charAt(i-1)) {
                 dp[i][0] = dp[i-1][0];
             }
         }
         for(int j = 1; j <= s1.length(); j++){
             if(s1.charAt(j-1) == s3.charAt(j-1))  {
                 dp[0][j] = dp[0][j-1];
             }
         }
         for(int i = 1; i<= s2.length(); i++){
             for(int j = 1; j <= s1.length(); j++){
                     dp[i][j] =  (dp [i-1][j] && s2.charAt(i-1) == s3.charAt(i + j -1) )
                                 ||(dp [i][j-1] && s1.charAt(j-1) == s3.charAt(i + j -1) );
             }
         }
      return  dp[s2.length()][s1.length()];
     }
 }
[leetcode]97. Interleaving String能否构成交错字符串的更多相关文章
- [LeetCode] 97. Interleaving String 交织相错的字符串
		Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ... 
- leetcode 97  Interleaving String ----- java
		Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ... 
- Leetcode#97 Interleaving String
		原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ... 
- 【一天一道LeetCode】#97. Interleaving String
		一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ... 
- 【LeetCode】97. Interleaving String
		Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ... 
- 【leetcode】Interleaving String
		Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ... 
- [LeetCode] Interleaving String 交织相错的字符串
		Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ... 
- 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串
		Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ... 
- 97. Interleaving String(字符串的交替连接  动态规划)
		Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ... 
随机推荐
- 第一节《Git初始化》
			创建版本库以及第一次提交 首先我看查看一下git的版本,本地的git是用的yum安装方式,如果想使用源码安装请参考官方文档. [root@git ~]# git --versiongit versio ... 
- redis hset  hmset过期时间
			hmset m k v > hset m k v (integer) > hget m k "v" > expire m (integer) > ttl m ... 
- idc市场
			机房 idc服务商 ============================== 电信1.古城热线-西部数据中心于2001年正式投入运营,有经济技术开发区和高新技术产业开发区两个核心机房高新路电信广场 ... 
- Java高级特性 第5节 序列化和、反射机制
			一.序列化 1.序列化概述 在实际开发中,经常需要将对象的信息保存到磁盘中便于检索,但通过前面输入输出流的方法逐一对对象的属性信息进行操作,很繁琐并容易出错,而序列化提供了轻松解决这个问题的快捷方法. ... 
- webpack 中,module、chunk、bundle 的区别(待补充)
			项目 区别 module 是开发中的单个模块 chunk 中文意思是"块",是指 webpack 在进行模块依赖分析的时候,代码分割出来的代码块 bundle 
- 【java】final修饰符介绍
			final: 最终,作为一个修饰符特点:1.可以修饰类,函数,变量2.被final修的的类不能被继承.因此类用final修饰可以避免被继承,被子类重写功能.3.被final修饰的方法不可以被重写.4. ... 
- 【python】dist-packages和site-packages的区别
			一.dist-packages和site-packages的区别 sudo apt-get install 安装的package存放在/usr/lib/python2./dist-packages目录 ... 
- 使用nrm工具高效地管理npm源
			在使用npm时,官方的源下载npm包会比较慢,国内我们基本使用淘宝的源,如果公司内部搭建了一套npm私有仓库,公司内部的源不可能把npm官方的npm包都同步,所以需要切换npm源.如果使用npm/cn ... 
- TCP/IP学习20180701-数据链路层-IP子网寻址
			IP-子网寻址IP地址是:网络号+主机号现在主机号都要求有子网号所以IP地址就变成了网络号+子网号+主机号例如一个B类地址:210.30.109.134210.30是网络号109是子网号134是主机号 ... 
- UE4:四种加载资源的方式
			转自:https://blog.csdn.net/zhangxsv123/article/details/79707686 第一种: 如果该蓝图有C++类(或者说是从C++类创建的蓝图),直接进行加载 ... 
