Given s1s2s3, 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.

使用DP, 过程如下图所示:

                            S2

S1

aadbbcbcac

0 “”

1 d

2 db

3 dbb

4 dbbc

5 dbbca

0 “”

T

F(d!=a)

F

F

F

F

1 a

T(a==a)

F(aa)

F

F

F

F

2 aa

T

T(aad)

T(aadb)

3 aab

F(aab!=aad)

T(aadb)

T(aadbb)

4 aabc

F

F(aadbb)

T(aadbbc)

5 aabcc

F

F(aadbbc)

发现某一格dp[i][j]为true只有当其上面或左边为true才行。且需要新加入的字母与s3新添加的字母一致,True状态才能延续。

代码:

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

简化为一维数组:

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

[Leetcode][JAVA] Interleaving String的更多相关文章

  1. 【leetcode】Interleaving String

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

  2. Java for LeetCode 097 Interleaving String 【HARD】

    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 ----- java

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

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

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

  5. 【leetcode】 Interleaving String (hard)

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

  6. [leetcode]97. Interleaving String能否构成交错字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...

  7. Leetcode#97 Interleaving String

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

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

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

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

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

随机推荐

  1. Git相关知识

    一些有用的链接: https://www.git-scm.com/ http://nvie.com/posts/a-successful-git-branching-model/ Git开发模式: 建 ...

  2. Git学习(一)——Git介绍与安装

    一.Git诞生 Linus在1991年创建了Linux,从此,Linux系统不断发展,成为最大的服务器系统软件. 2005年,Linus用C编写了一个分布式版本控制工具--Git. 二.集中式vs分布 ...

  3. ios pod库更新到1.0或1.0.1之正确修改podfile文件

    今天看到cocopods都更新到1.0.1了,之前什么时候更新到的1.0都没发现,刚刚更新一下之后,立马出现了一大堆的错误. 如果没有更新的话,建议不要更新!!!书写麻烦了! 现在记录如何一步一步去除 ...

  4. NMAP 基本用法

    Nmap 用途: 1.通过对设备或者防火墙的探测来审计它的安全性. 2.探测目标主机所开放的端口. 3.网络存储,网络映射,维护和资产管理.(这个有待深入) 4.通过识别新的服务器审计网络的安全性. ...

  5. LVS_DR模式构建配置

    一.环境准备 lvs负载均衡器 系统:centos6.8 ip:192.168.2.203 vip:192.168.2.17 web服务器RS1 系统:centos6.8 ip:192.168.2.2 ...

  6. Redis与Memcached的区别

    传统MySQL+ Memcached架构遇到的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量 ...

  7. 拓扑排序(topsort)

    本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...

  8. Neutron Kilo-Liberty-Mitaka各版本区别

    http://blog.csdn.net/bc_vnetwork/article/details/51848623

  9. c# 写着玩的,两个Task并发,一个写队列一个读队列的异常情况

    class Program { class TestEnqueue { static Queue<string> str = new Queue<string>(); publ ...

  10. [PHP] Xhprof 非侵入式使用指南

    一般使用 Xhprof ,按文档操作可以快速上手,文件头开启 Xhprof,应用结束处得到访问的url查看. 这种使用方式可以快速看到效果,同时也有一些不好的地方: 一是不利于重复利用写好的示例代码: ...