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. 在VMware上安装ubuntu,并且SecureCRT远程连接

     工具: VMware:VMware-workstation-full_12.5.5.17738.exe Ubuntu镜像:ubuntu-16.04-server-amd64.iso 远程连接工具-- ...

  2. 假如时光倒流,我会这样学习Java

    回头看看, 我进入Java 领域已经快15个年头了, 虽然学的也一般, 但是分享下我的心得,估计也能帮大家少走点弯路. [入门] 我在2001年之前是C/C++阵营, 有C和面向对象的基础, 后来转到 ...

  3. Java IO学习笔记七

    System对IO的支持 System是系统的类,其中的方法都是在控制台的输入和输出,但是通过重定向也是可以对文件的输入输出 System中定义了标准输入.标准输出和错误输出流,定义如下: stati ...

  4. Go - concurrency

    并发 vs 并行 首先,我们先来搞清楚概念以及并发和并行的区别. 并发 - 利用时间片切换来实现“同时”运行的. 并行 - 利用CPU的多核来实现通过多线程来实现同时运行. Go 语言的设计理念就是通 ...

  5. Lucence

    Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引 ...

  6. MVC之前-ASP.NET初始化流程分析1

    Asp.net Mvc是当前使用比较多的web框架,也是比较先进的框架.我打算根据自己的实际项目经验以及相关的源码和一些使用Asp.net Mvc的优秀项目(主要是orchard)来说一说自己对于As ...

  7. Linux: 安装NVIDIA显卡驱动

    Linux(Fedora25, 64bit)台式机配备了NVIDIA显卡GTX950,但是仅仅使用开源驱动nouveau,无法发挥NVIDIA显卡的性能,所以可以考虑使用官方提供的显卡驱动. # 先安 ...

  8. Spring两种代理区别

    Spring的两种代理JDK和CGLIB的区别浅谈: Java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用invokeHandler类来处理: 而cglib动态代理是利用a ...

  9. JAVA容器结构图

  10. ng-cordova(插件库)

    ng-cordova 环境配置 1.执行以下命令 bower install ngCordova 2.引用文件(在引用cordova.js之前引用) <script src="lib/ ...