Interleaving String——Leetcode
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.
题目大意:给定三个字符串,s1,s2,s3,问s3是否由s1和s2交叉构成。
解题思路:递归超时,重复计算太多,加上cache可能可以。
说一下dp的方法,研究了半天才看明白,用一个二维布尔数组dp[i][j]表示从s1中取前i位,从s2中取前j位构成s3的前i+j位是否可以。
那么dp[i][j]可能从dp[i-1][j]或dp[i][j-1]而来:
如果从dp[i-1][j]来,那么dp[i-1][j]应该为true,并且s1.charAt(i-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;
如果从dp[i][j-1]来,那么dp[i][j-1]应该为true,并且s2.charAt(j-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;
初始化,dp[0][0]=true表示两个空字符串构成一个空字符串为true。
另外,从s1或s2开始构成s3的前n或m个字符也是初始条件。
举个列子,注意左上角那一对箭头指向的格子dp[1][1], 表示s1取第1位a, s2取第1位d,是否能组成s3的前两位aa
从dp[0][1] 往下的箭头表示,s1目前取了0位,s2目前取了1位,我们添加s1的第1位,看看它是不是等于s3的第2位,( i + j 位)
从dp[1][0] 往右的箭头表示,s1目前取了1位,s2目前取了0位,我们添加s2的第1位,看看它是不是等于s3的第2位,( i + j 位)
public static boolean isInterleave(String s1, String s2, String s3) {
if (s3.length() != s1.length() + s2.length()) {
return false;
}
boolean[][] dp = new boolean[s1.length() + 1][s2.length() + 1];
dp[0][0] = true;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == s3.charAt(i)) {
dp[i + 1][0] = dp[i][0];
}
}
for (int i = 0; i < s2.length(); i++) {
if (s2.charAt(i) == s3.charAt(i)) {
dp[0][i + 1] = dp[0][i];
}
}
for (int i = 1; i < s1.length() + 1; i++) {
for (int j = 1; j < s2.length() + 1; j++) {
dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
return dp[s1.length()][s2.length()];
}
参考:http://blog.csdn.net/u011095253/article/details/9248073
Interleaving String——Leetcode的更多相关文章
- 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 java
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...
- [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 ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
随机推荐
- devenv compile errors collection
任务:使用 devenv commnd line 编译 VS 2010 工程. 使用 devenv 编译工程,要保证工程所需的 VC++目录 (VC++ Directories) 设置正确才能编译成功 ...
- ubuntu ll命令
用过 Redhat 的朋友应该很熟悉 ll 这个命令,就相当于 ls -l,但在 Ubuntu 中就不行了.严格来说 ll 不是一个命令,只是命令的别名而已.很多 Linux 用户都使用 bash s ...
- umount 卸载的时候,提示busy!
mount /dev/sdb /mnt/disk umount -l /mnt/disk[有busy的问题可以加上l项] 1. 查询当前谁在使用device,fuser /mnt/temp,查询结果是 ...
- validate插件的使用
方法如下: 插件: jquery.validate.js jquery.validate.custom.js bootstrap html代码: <form id="form_name ...
- 回答了个问题,9x9 乘法表生成器
# -*- coding: utf-8 -*- from prettytable import PrettyTable pt = PrettyTable() # 需要安装prettytable这个库来 ...
- Docker 安装jupyter notebook
1. 利用image运行一个container sudo docker run -it --net=host tingting --net=host:让container可以上网,安装原来的sudo ...
- IIS 500 – 内部服务器错误解决方案
最近装了测试机windows2008使用IIS7.5各种不习惯呀,各种问题,唉.. 今天又遇到了“500 – 内部服务器错误. 您查找的资源存在问题,因而无法显示.”的问题,网上查找了一下,找到解决办 ...
- 读取spring配置文件的方法(spring读取资源文件)
1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...
- maven 搭建企业级web项目
就看这篇文章了:http://www.cnblogs.com/quanyongan/archive/2013/05/28/3103243.html
- 9.png(9位图)在android中作为background使用导致居中属性不起作用的解决方法
在使用到9.png的布局上面添加 android:padding="0dip" 比如 <LinearLayout android:layout_widt ...