[Leetcode][JAVA] 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.
使用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的更多相关文章
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 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 ...
- 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 = ...
- [LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...
- 【leetcode】 Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
- Leetcode#97 Interleaving String
原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
随机推荐
- sql server 2008 外键关联的设置和取消
直接上图片 选中表右击-设计 找到需要设置外键的字段.右击-关系,在弹出的对话框中点击添加 选择右边的小按钮点击.选择主键表和关联的主键ID,以及外建表的关联字段. 建立外键完成. 删除的话选中某个外 ...
- centos7 install 安装mysql
# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -ivh mysql-community- ...
- php js => splice 数组 插入 功能
php array_splice 手册详解 array_splice - 把数组中的一部分去掉并用其它值取代 参数 input 输入的数组. offset 如果 offset 为正,则从 i ...
- highlight.js 页面 代码高亮
官网:https://highlightjs.org/ 功能: 支持 155 种编程语言的语法解析:拥有 73 种样式 自动检测编程语言 可以在 node.js 平台上运行 支持各种标签 与任何 js ...
- Android停止运行问题1_layout布局xml问题
好好的写了300多行布局文件代码,写完之后运行结果,停止运行了.我当时就奇怪,xml有错误应该会提示啊,我就一个一个的缩小错误的代码范围,先直接换成一个简单的TextView ,运行一下没有错误.再慢 ...
- [转]VS2013自带SQL Server 的启用方法
本文转自:http://www.icharm.me/vs2013%E8%87%AA%E5%B8%A6%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93sql-server-exp ...
- sql脚本查询日期时间段日期
---列举指定时间月份DECLARE @date1 VARCHAR(10) , @date2 VARCHAR(10)SET @date1 = '2010-01-01'SET @date2 = '201 ...
- WNDR3700V4恢复原厂固件(使用TFTP刷网件原厂固件)
WNDR3700v4原厂固件下载地址: http://support.netgear.cn/doucument/More.asp?id=2203 操作方法: 1.将设备断电: 2.按住设备背面的Res ...
- Golang 文件服务器小结
花了一个星期学习文件服务器,老是在一些地方搞混,整理一下所学的,清晰了不少. 学Go半个月,还有很多不懂的地方,有理解错误的,还望高手指出. 注:以下代码中,w为http.ResponseWriter ...
- 进阶版css的点滴
-moz-:firefox: -webkit- Safari 和 Chrome