[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 ...
随机推荐
- 重温WCF之构建一个简单的WCF(一)(1)通过控制台和IIS寄宿服务
一.理解什么是WCFWCF就是.NET平台下各种分布式技术的集成,并提供了一套统一的编程接口 二.WCF的定义WCF(Windows Communication Foundation)是微软为构建面向 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- 第四篇:SOUI资源文件组织
什么是资源? 现代的软件只要有UI,基本上少不了资源. 资源是什么?资源就是在程序运行时提供固定的数据源的文件. 在MFC当道的时代,资源一般就是位图(Bitmap),图标(Icon),光标(Curs ...
- Request和Response对象
Request 和 Response 对象起到了服务器与客户机之间的信息传递作用.Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏 ...
- eclipse中快捷键
转为大写 ctrl+shift+x 转为小写 ctrl+shift+y 根据类名快速找到类文件 ctrl+shift+r 返回上一级 ALT + <- 速定位到某一行 ctrl+L
- [Liferay6.2]核心配置文件portal.properties
portal.properties是liferay中一个非常核心的配置文件.我们可以在liferay源代码或者解压liferay部署包中的portal-impl.jar中获得.以liferay6.2为 ...
- java基础之——类的初始化顺序
由浅入深,首先,我们来看一下,一个类初始化有关的都有些啥米: 静态成员变量.静态代码块.普通成员变量.普通代码块.构造器.(成员方法?貌似跟初始化没有啥关系) 现在我们来看看她们的初始化顺序, 从性质 ...
- UI中 frame 与 transform的用法与总结
在iOS中,我们是不可以直接访问控件中frame的结构体的成员的,因此我们需要分三步来改变一个UI控件的位置,大小 一.frame用法 frame的结构体类型为: struct CGRect { CG ...
- 求余VS求模--C语言中表述
之前看帖子,发现许多时候基本上大家都把求模和求余混为一谈了.但实际上二者的概念是有区别的 1. 求余 在C语言中,求余对应的操作符是%,且a%b求余的最后结果总是与a符号相同,最后的数值为|a|% ...
- PHP 配置文件详解(php.ini 详解 )
[PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...