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. phpmyadmin nginx设置

    1,解压缩phpmyadmin4.2.8压缩包到/usr/local/phpMyAdmin 2,复制config.sample.inc.php为config.inc.php 3,修改nginx.con ...

  2. 20165308实验三 敏捷开发与XP实践实验报告

    实验三 敏捷开发与XP实践实验报告 实验目的 安装 alibaba 插件,解决代码中的规范问题.再研究一下Code菜单,找出一项让自己感觉最好用的功能. 在码云上把自己的学习搭档加入自己的项目中,确认 ...

  3. OkHttp官方中文文档

    https://blog.csdn.net/jackingzheng/article/details/51778793 https://www.cnblogs.com/ldq2016/p/879630 ...

  4. Linux platform平台总线、平台设备、平台驱动

    平台总线(platform_bus)的需求来源? 随着soc的升级,S3C2440->S3C6410->S5PV210->4412,以前的程序就得重新写一遍,做着大量的重复工作, 人 ...

  5. AIOps指导

       AIOps代表运维操作的人工智能(Artificial Intelligence for IT Operations), 是由Gartner定义的新类别,Gartner的报告宣称,到2020年, ...

  6. Linux系统时钟的更改

    linux系统时钟有两个,一个是硬件时钟,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时钟,是linux系统Kernel时间. 查看.设置硬件时间: 查看系统硬件时钟 hwclo ...

  7. 02-Response简单响应报文

    package com.day5; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputSt ...

  8. matplotlib绘图总结《转》

    本文作为学习过程中对matplotlib一些常用知识点的整理,方便查找. 类MATLAB API 最简单的入门是从类 MATLAB API 开始,它被设计成兼容 MATLAB 绘图函数. from p ...

  9. Git从远程clone项目报错cannot open git-upload-pack,将http.sslVerify设为false即可

    通过HTTPS访问Git远程仓库,如果服务器的SSL证书未经过第三方机构签署,那么Git就会报错 通过https访问Git远程仓库,如果服务器的SSL证书没有经过第三方机构签署,就会出现cannot ...

  10. seleniuim面试题1

    seleniuim面试题1 乙醇 创建于 4 个月 之前 最后更新时间 2018-09-11 selenium中如何判断元素是否存在? selenium中没有提供原生的方法判断元素是否存在,一般我们可 ...