[Leetcode] 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.
Solution 1: 指针的做法。。。错误!
比如例子: 在这种情况下,既可以进入s1,又可以进入s2,到底该进哪个呢,不能定。
| Input: | "aa", "ab", "abaa" |
| Output: | false |
| Expected: | true |
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if(s1==null||s1.length()==0)
return s2.equals(s3);
if(s2==null||s2.length()==0)
return s1.equals(s3);
int l1=s1.length();
int l2=s2.length();
int l3=s3.length();
if(l3!=(l1+l2))
return false;
int index1=0;
int index2=0;
int index3=0;
while(index1<l1&&index2<l2){
if(s1.charAt(index1)==s3.charAt(index3)){
index1++;
index3++;
}else if(s2.charAt(index2)==s3.charAt(index3)){
index2++;
index3++;
}else{
return false;
}
}
return true;
}
}
Solution 2: DP.
http://www.cnblogs.com/springfor/p/3896159.html
“When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”
所以这道题还是用DP的思想解决。
大体思路是,s1取一部分s2取一部分,最后是否能匹配s3。
动态规划数组是dp[i][j],表示:s1取前i位,s2取前j位,是否能组成s3的前i+j位。
初始化是,假设s1为空,那么s2每一位跟s3匹配放入dp[0][j];假设s2为空,那么s1每一位跟s3匹配放入dp[i][0]。
下面就继续匹配。讲解引用自: http://blog.csdn.net/u011095253/article/details/9248073
“
那什么时候取True,什么时候取False呢?
False很直观,如果不等就是False了嘛。
那True呢?首先第一个条件,新添加的字符,要等于s3里面对应的位( i + j 位),第二个条件,之前那个格子也要等于True
举个简单的例子s1 = ab, s2 = c, s3 = bbc ,假设s1已经取了2位,c还没取,此时是False(ab!=bb),我们取s2的新的一位c,即便和s3中的c相等,但是之前是False,所以这一位也是False
同理,如果s1 = ab, s2 = c, s3=abc ,同样的假设,s1取了2位,c还没取,此时是True(ab==ab),我们取s2的新的一位c,和s3中的c相等,且之前这一位就是True,此时我们可以放心置True (abc==abc)
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1 == null || s1.length() == 0)
return s2.equals(s3);
if (s2 == null || s2.length() == 0)
return s1.equals(s3);
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
if (l3 != (l1 + l2))
return false;
boolean[][] state = new boolean[l1 + 1][l2 + 1];
state[0][0]=true;
for(int i=1;i<=l1;++i){
state[i][0]=state[i-1][0]&&(s1.charAt(i-1)==s3.charAt(i-1));
}
for(int i=1;i<=l2;++i){
state[0][i]=state[0][i-1]&&(s2.charAt(i-1)==s3.charAt(i-1));
}
for(int i=1;i<=l1;++i){
for(int j=1;j<=l2;++j){
state[i][j]=(state[i][j-1]&&(s2.charAt(j-1)==s3.charAt(i+j-1)))||(state[i-1][j]&&(s1.charAt(i-1)==s3.charAt(i+j-1)));
}
}
return state[l1][l2];
}
}
[Leetcode] Interleaving String的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [LeetCode] Interleaving String 解题思路
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- [leetcode]Interleaving String @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
随机推荐
- HTML5学习之跨文档传输消息(七)
新标准中提供了文档之间直接的消息传输API.而且不限制跨域消息传递! 发送消息使用的是Window对象的postMessage(data,targetURL)方法就可以了,但给哪个window对象发送 ...
- python 之socket 网络编程
socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Un ...
- Filp Game
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25573 Accepted: 11052 题目链接: ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- Oracle备份 还原命令
1.备份命令 exp username/password file=d:/test/test.dmp; 2.还原命令 imp username/password full=y file=d:/test ...
- JavaScript中call,apply和prototype
[TOC] call()方法 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 如果没有提供 thi ...
- C# 指针操作图像 细化处理
/// <summary> /// 图形细化 /// </summary> /// <param name="srcImg"></para ...
- ios 数据类型转换 UIImage转换为NSData NSData转换为NSString
1.UIImage转换为NSData NSData *data;if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepr ...
- 【工具】Git
1.安装好Git以后,在开始菜单里找到Git->Git Bash,弹出一个命令窗口 2.设置邮箱 . 3.创建文件夹 4.创建版本库 5.将文件添加到缓存区中去 6.提交文件 7.检查是否还有文 ...
- python操作excel表格(xlrd/xlwt)
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...