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. web前端常用代码于面试等资源

    https://www.cnblogs.com/moqiutao/p/4766146.html

  2. git bash的安装与配置

    作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 1.下载安装配置用户名和邮箱. (1)下载安装Github配置 ...

  3. Spring IOC(四)总结升华篇

    本系列目录 Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结升华 =============正文分割线===== ...

  4. HBase各版本对Hadoop版本的支持情况

    转载自:http://blog.csdn.net/sunny05296/article/details/54089194 安装HBase时,要考虑选择正确的Hadoop版本,否则可能出现不兼容的情况. ...

  5. 获得驱动器信息卷设备&&Ring3得到磁盘文件系统(NTFS WIN10)

    // GetLogicalDriveStrings.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows ...

  6. processjs Documentation

    Documentation   Paul Nieuwelaar edited this page on 20 Sep 2017 · 4 revisions Installation & Usa ...

  7. 关于AsyncSocket

               写篇博客,在我项目中用到了一个很重要的第三方---AsyncSocket,写下我对AsyncSocket使用心得.我的项目中是APP对硬件直接交互,APP对硬件发指令的时候不需要 ...

  8. 如何查看安装python和numpy的版本

    命令行下查看python和numpy的版本和安装位置 1.查看python版本 方法一: python -V 注意:‘-V‘中‘V’为大写字母,只有一个‘-’ 方法二: python --versio ...

  9. rabbitmq (三) 发布/订阅

    rabbitmq的目的并不是让生产者把消息直接发到队列里面去, 这样不能实现解耦的目的,也不利于程序的扩展. 所以就有交换机(exchanges)的概念. 交换机有几种类型:direct, topic ...

  10. [Unity优化]UI优化(二):Mask组件分析

    参考链接: https://www.sohu.com/a/211665096_99940808 1.Mask组件实现原理 使用模板测试,一方面使Mask对象所在区域的模板缓冲值置为1,另一方面使被Ma ...