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. spring web.xml 难点配置总结

    web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...

  2. MSSQL附加数据库5120错误(拒绝访问)处理方法

    一. 右键需要附加的数据库文件,弹出属性对话框,选择安全标签页. 找到Authenticated Users用户名. 如未找到,进行Authenticated Users用户名的添加. 二. 添加Au ...

  3. JQuery源码解析--callbacks

    (function (global, factory) { factory(global); })(this, function (window, noGlobal) { var rootjQuery ...

  4. mouseChildren启示

    将aSprite的 mouseChildren 属性设置为 false ,可以实现mouseClick 事件的目标为 aSprite对象,而不是其子对象中的任一个.

  5. linux之find命令详解

    linux之find命令详解 查找文件find ./ -type f查找目录find ./ -type d查找名字为test的文件或目录find ./ -name test查找名字符合正则表达式的文件 ...

  6. 关于Servlet手动配置web.xml部分代码

    <servlet> <!-- 文件名 --> <servlet-name>deleteServlet</servlet-name> <!-- 文件 ...

  7. WPF 中使用MVVM模式后,找回ListBox中的ListBoxItem元素

    ListBoxItem lstitem = this.list.ItemContainerGenerator.ContainerFromItem(m) as ListBoxItem; 其中this.l ...

  8. Ubuntu 安装tftp服务器

    Ubuntu下搭建tftp服务器最简单方法   转 linux公社       今天开始调试ARM的板子,要通过tftp下载到板子上,所以又要配置tftp服务器,真的烦死了... (本人酷爱装系统,所 ...

  9. Flask 备注一(单元测试,Debugger, Logger)

    Flask 备注一(单元测试,Debugger, Logger) Flask是一个使用python开发Web程序的框架.依赖于Werkzeug提供完整的WSGI支持,以及Jinja2提供templat ...

  10. 选择本地照片之后即显示在Img中(客户体验)

    最近转战MVC项目,然后又再次遇到照片上传的实现,之前都是使用ASP.NET,虽然也有照片上传,而且出于客户体验考虑, 也实现了选择本地照片之后即时显示在IMG中,在这里就简单介绍其实现(ASP.NE ...