Given s1s2s3, 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能否构成交错字符串的更多相关文章

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

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...

  2. 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 = ...

  3. Leetcode#97 Interleaving String

    原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...

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

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

  5. 【LeetCode】97. Interleaving String

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

  6. 【leetcode】Interleaving String

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

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

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

  8. 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 = ...

  9. 97. Interleaving String(字符串的交替连接 动态规划)

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

随机推荐

  1. PythonStudy——汇编语言 Assembly Language

    汇编语言 汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操 ...

  2. Announcing the Operate Preview Release: Monitoring and Managing Cross-Microservice Workflows

    转自:https://zeebe.io/blog/2019/04/announcing-operate-visibility-and-problem-solving/   Written by Mik ...

  3. Python3之max key参数学习记录

    今天用Python写脚本,想要实现这样的功能:对于给定的字典,返回其中Value最大值对应的Key. 搜索后找到了解决方法,同时也学到了max key参数的作用. 例1, testlist = [9. ...

  4. 在线HTML编辑器KindEditor

     简介 KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome.Safari.Opera等主流浏览器.KindEdi ...

  5. nodeppt:网页版 PPT

    资料 网址 github https://github.com/ksky521/nodeppt 网页版PPT(nodeppt 的介绍) http://deliazhi.com/2017/03/31/W ...

  6. Aria2+百度网盘 无限制的下载神器

    Aria2是一款免费开源跨平台且不限速的多线程下载软件,Aria2的优点是速度快.体积小.资源占用少:支持 HTTP / FTP / BT / Magnet 磁力链接等类型的文件下载:支持 Win.M ...

  7. django 路由系统中name应用

    作用:对URL路由关系进行命名, ***** 以后可以根据此名称生成自己想要的URL ***** name的两大应用 url(r'^asdfasdfasdf/', views.index, name= ...

  8. python:带参数的装饰器,函数的有用信息

    一.带参数的装饰器,函数的有用信息 def func1(): '''此函数的功能是完成的登陆的功能 return: 返回值是登陆成功与否(true,false) ''' print(333) func ...

  9. git一个分布式版本工具的使用

    1.git和cvs的区别 分支更快,更容易 支持离线工作,本地提交可以稍后提交到服务器上 git提交是原子的,且是整个项目范围的,而不像cvs是对每个文件 git中的每个工作树都包含一个具有完整项目历 ...

  10. sync;sync;sync;reboot

    Sync命令 在用reboot命令启动unix系统后,系统提示出错信息,部分应用程序不能正常工作.经仔细检查系统文件,并和初始的正确备份进行比较,发现某些文件确实被破坏了,翻来覆去找不到文件遭破坏的原 ...