[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 ...
随机推荐
- Delphi的面向对象编程基础笔记
1.面向对象.一门面向对象的编程语言至少要实现以下三个OOP的概念 封装:把相关的数据和代码结合在一起,并隐藏细节.封装的好处是利用程序的模块化,并把代码和其他代码分开 继承:是指一个新的类能够从父类 ...
- SSH入门简单搭建例子
因为公司涉及项目使用SSH,为了解SSH搭建方式和运作原理,就自己搭建了一个. 采用尽量以最少的JAR包,搭建一个简单的struts2+spring+hibernate环境,希望像我这样的入门者都能理 ...
- .Net Ioc Unity
Unity 的接口IUnityContainer public interface IUnityContainer : IDisposable IUnityContainer RegisterType ...
- <转>WCF实例化模式与高并发处理
WCF实例化模式与高并发控制 1.实例化模式InstanceModel 1.1 PerCall:单调模式 每次调用都会产生一个实例 例[ServiceBehavior(InstanceContextM ...
- linux下的c编程
linux下的c编程 Linux 系统上可用的 C 编译器是 GNU C 编译器, 它建立在自由软件基金会的编程许可证的基础上,因此可以自由发布.GNU C 对标准 C 进行一系列扩展,以增强标准 ...
- DWZ分页、排序失效小结
1. 在视图文件中与分页相关的代码段 <form id="pagerForm" method="post" action="w_list.htm ...
- 数据分析(3):ufunc
universal function 可以对数组里的每一个元素进行操作,底层是C语言实现的,在对数组运算时表现卓越 1.1 初步上手 x = np.linspace(0,2*np.pi,10) y = ...
- 初探YAML
YAML何许物也?在XML泛滥的情况下,YAML的出现的确让人眼前一亮,在初步学习了YAML以后,粗略的总结了一下,拿出来和大家分享.[MindMap][参考文档]YAML Specification ...
- Activity之间传递数据的方式及常见问题总结
Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...
- loadrunner中切割字符串
下面函数的作用: http://blog.csdn.net/hgj125073/article/details/8447605 通过-与: 字符切割字符串,即-与:字符已经被\0 字符取代 char ...