Interleaving String leetcode java
题目:
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.
题解:
这道题还是像之前我引过的那句话:
“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)
”
代码如下:
1 public boolean isInterleave(String s1, String s2, String s3) {
2 if(s3.length()!=s1.length()+s2.length())
3 return false;
4
5 boolean [][] dp = new boolean [s1.length()+1][s2.length()+1];
6 dp[0][0]=true;
7
8 for(int i = 1; i<=s1.length() && s1.charAt(i-1)==s3.charAt(i-1); i++)
9 dp[i][0]=true;
for(int i = 1; i<=s2.length() && s2.charAt(i-1)==s3.charAt(i-1); i++)
dp[0][i]=true;
for(int i = 1; i <= s1.length(); i++){
for(int j = 1; j <= s2.length(); j++){
char c = s3.charAt(i+j-1);
if(c == s1.charAt(i-1) && dp[i-1][j])
dp[i][j] = true;
if(c == s2.charAt(j-1) && dp[i][j-1])
dp[i][j] = true;
}
}
return dp[s1.length()][s2.length()];
}
Reference:
http://blog.csdn.net/u011095253/article/details/9248073
http://www.cnblogs.com/lichen782/p/leetcode_interleaving_string.html
Interleaving String leetcode java的更多相关文章
- Interleaving String leetcode
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- Interleaving String——Leetcode
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- Scramble String leetcode java
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- Reverse Words in a String leetcode java
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
- [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 ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【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 ...
随机推荐
- GraphicsMagick为图片添加水印
GraphicsMagick号称图像处理领域的瑞士军刀.提供了健壮及高效的图像处理工具包和库,支持超过88种主流图片格式包括:BMP,GIF,JPEG,JPEG-2000,PNG,PDF,PNM,TI ...
- How can I pretty-print JSON in python?
python -m json.tool my_json.json 转自: http://stackoverflow.com/questions/352098/how-can-i-pretty-prin ...
- 【GoLang】GoLang 微服务、开源库等参考资料
参考资料: GoLang书籍: https://github.com/dariubs/GoBooksGo名库: https://github.com/Unknwon/go-rock-libraries ...
- 【Spring】Spring系列2之bean的配置
2.bean的配置 2.1.IOC概述 2.2.bean的获取 2.3.依赖注入方式 2.4.属性注入细节 内部bean,不需要ID,ID无效,外部不能引用: 2.5.集合属性注入 2.6.使用p命名 ...
- iOS 中constraint 不等于约束和低优先级约束使用的简单体会
看了些文章发现,在使用constraint时,不等于约束往往是和低优先级约束成对使用的,这样才能实现他们的效果. 看看例子 下面是在3.5存屏幕下的效果 图1,竖屏,在满足>=50的前提下,可以 ...
- Ubuntu13.04 安装 chrome
1.chrome官网下载deb安装包:https://www.google.com/intl/zh-CN/chrome/browser/ 2.进入下载好的目录执行:sudo dpkg -i googl ...
- cf158B(水题)
题意:1辆出租车可以坐4人,已知k组人每组ki(ki<=4)人去坐车,要求同组人坐同一辆车,求最少需多少辆车.. 4人组的单独算,1人组和3人组一起,如1多余再将1和2匹配即可.... 代码如下 ...
- C++实现大数据乘法
结构体定义与封装 struct bigdatacom { private : ]; ]; public : void init(const char *str1,const char *str2) { ...
- javaweb数据库操作
本文主要内容有C3P0数据库连接池,dbutils的使用,元数据的应用 在对数据库进行增删改查时,使用数据库连接池可以有效的提高效率,节省资源,C3P0是Apache组织提供的一个有效方式 C3P0的 ...
- 基于Twemproxy的Redis集群方案
概述 由于单台redis服务器的内存管理能力有限,使用过大内存redis服务器的性能急剧下降,且服务器发生故障将直接影响大面积业务.为了获取更好的缓存性能及扩展型,我们将需要搭建redis集群来满足需 ...