Interleaving String

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.

标签: Dynamic Programming String

分析:动态规划;设boolean数组dp[i][j]表示字符串s1[0...i-1]和字符串s2[0...j-1]是否可以匹配到字符串s3[0...i+j-1],

因此有字符s3[i+j-1]可以由字符s1[i-1]或字符s2[j-1]来匹配,所以:

如果s1[i-1]==s3[i+j-1],则dp[i][j]=dp[i-1][j];

如果s2[j-1]==s3[i+j-1],则dp[i][j]=dp[i][j-1];

所以状态转移方程为:dp[i][j]=(dp[i-1][j]&&s1[i-1]==s3[i+j-1])||(dp[i][j-1]&&s2[j-1]==s3[i+j-1])

参考代码:

public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
int len1=s1.length();
int len2=s2.length();
int len3=s3.length();
if(len1+len2!=len3)
return false;
boolean dp[][]=new boolean[len1+1][len2+1];
dp[0][0]=true;
for(int j=1;j<=len2;j++){
dp[0][j]=dp[0][j-1]&&s2.charAt(j-1)==s3.charAt(j-1);
}
for(int i=1;i<=len1;i++){
dp[i][0]=dp[i-1][0]&&s1.charAt(i-1)==s3.charAt(i-1);
}
for(int i=1;i<=len1;i++){
for(int j=1;j<=len2;j++){
dp[i][j]=(dp[i-1][j]&&s1.charAt(i-1)==s3.charAt(i+j-1))||(dp[i][j-1]&&s2.charAt(j-1)==s3.charAt(i+j-1));
}
}
return dp[len1][len2];
}
}

LeetCode-Interleaving String[dp]的更多相关文章

  1. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  2. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

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

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

  4. [Leetcode] Interleaving String

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

  5. [LeetCode] Interleaving String 解题思路

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

  6. [LeetCode] Interleaving String [30]

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

  7. [leetcode]Interleaving String @ Python

    原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...

  8. Interleaving String (DP)

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

  9. 【leetcode】Interleaving String

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

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

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

随机推荐

  1. maven插件打包可执行jar

    pom文件加 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</grou ...

  2. centos文件权限详解

    假设回显信息为  ①-②rws③r-x④r-x ⑤1 ⑥root ⑦root ⑧430540 ⑨Dec 20 18:27 ⑩/usr/sbin/passwd ,现在逐一分析其内容. ①. 首字符-,表 ...

  3. 第二章完结,包含exam练习

    正则方程(Normal Equation) 梯度下降是最小化代价函数\(J(\theta)\)的一种方式,这里提出了另一种方式即正则方式不使用迭代方式:\(\theta = (X^TX)^{-1}X^ ...

  4. python制作pdf电子书

    python制作pdf电子书 准备 制作电子书使用的是python的pdfkit这个库,pdfkit是 wkhtmltopdf 的Python封装包,因此在安装这个之前要安装wkhtmltopdf 安 ...

  5. 移动端APP CSS Reset及注意事项CSS重置

    @charset "utf-8"; * { -webkit-box-sizing: border-box; box-sizing: border-box; } //禁止文本缩放 h ...

  6. Linux 最新SO_REUSEPORT特性

    1.前言 昨天总结了一下Linux下网络编程“惊群”现象,给出Nginx处理惊群的方法,使用互斥锁.为例发挥多核的优势,目前常见的网络编程模型就是多进程或多线程,根据accpet的位置,分为如下场景: ...

  7. Linux下php安装memcache

    Linux下php安装memcache说明:php安装目录:/phpstudy/server/php/bin/phpphp.ini配置文件路径:/phpstudy/server/php/etc/php ...

  8. LVS工作模式与调度算法

    LVS三种工作模式.十种调度算法介绍 工作模式介绍: 1.Virtual server via NAT(VS-NAT) 优点:集群中的物理服务器可以使用任何支持TCP/IP操作系统,物理服务器可以分配 ...

  9. 打造一个简单的Java字节码反编译器

    简介 本文示范了一种反编译Java字节码的方法,首先通过解析class文件,然后将解析的结果转成java代码.但是本文并没有覆盖所有的class文件的特性和指令,只针对部分规范进行解析. 所有的代码代 ...

  10. linux查询进程号,出现两个进程

    [root@ADM01B ~]# ps -ef|grep iesmgr root 5929 5321 0 09:38 pts/7 00:00:00 grep iesmgr root 9798 1 0 ...