动态规划之97 Interleaving String
题目链接:https://leetcode-cn.com/problems/interleaving-string/description/
参考链接:https://blog.csdn.net/u011095253/article/details/9248073
https://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[i][j]表示s1取前i位,s2取前j位,是否能组成s3的前i+j位。
比如:s1="aabcc" s2="dbbca" s3="aadbbcbcac"
dp的数组如上图所示。
dp[0][0]=1;
dp[0][1]:使用s1的第一个字符'1'可以组成s3的第一个字符。然后第二也是如此;aab!=aad。后面的也是一样。

对于dp[i][i]的状态显然是由两个方向的状态来决定的。dp[i][i]是由dp[i-1][j]和dp[i][j-1]来决定的。
public boolean isInterleave(String s1, String s2, String s3) {
if (s1 == null || s2 == null || s3 == null) return false;
if (s1.length() + s2.length() != s3.length()) return false;
int dp[][]=new int[s1.length() + 1][s2.length() + 1];
dp[0][0]=1;
for (int i = 1; i < dp.length; i++) {
if (s1.charAt(i-1)==s3.charAt(i-1)&&dp[i-1][0]==1) {
dp[i][0]=1;
}
}
for (int i = 1; i < dp[0].length; i++) {
if (s2.charAt(i-1)==s3.charAt(i-1)&&dp[0][i-1]==1) {
dp[0][i]=1;
}
}
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (s1.charAt(i - 1) == s3.charAt(i + j - 1) && dp[i - 1][j]==1) {
dp[i][j] = 1;
}
if (s2.charAt(j - 1) == s3.charAt(i + j - 1) && dp[i][j - 1]==1) {
dp[i][j] = 1;
}
}
}
return dp[dp.length-1][dp[0].length-1]==1;
}
动态规划之97 Interleaving String的更多相关文章
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 97. Interleaving String(字符串的交替连接 动态规划)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...
- leetcode 97 Interleaving String ----- java
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 97. Interleaving String
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- [leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
- 97. Interleaving String (String; DP)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- 18.搭建 vue 环境
第一步 node环境安装 1.1 如果本机没有安装node运行环境,请下载node 安装包进行安装1.2 如果本机已经安装node的运行换,请更新至最新的node 版本下载地址:https://nod ...
- sqli-labs(六)
第十一关: 这关是一个登陆口,也是一个sql注入的漏洞,也就是常说的万能密码. 在输入框账号密码种分别输入 1' 和1' 页面会报错. 后台使用的单引符号进行的拼接.账号输入1' or '1'=' ...
- jQuery-图片放大镜
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 环形数组 最大子段和 dp
题目链接:https://nanti.jisuanke.com/t/36118 环形数组的连续最大子段和,有两种情况. 1.最大和的这个子段没有包含头尾.所以直接dp[i] = max(dp[i-1] ...
- jQuery样式--css(name|pro|[,val|fn])
css(name|pro|[,val|fn]) 概述 访问匹配元素的样式属性 参数 name 要访问的属性名称 name 一个或多个CSS属性组成的一个数组 properties 要设置为样式属 ...
- mysql 问题:Unknown system variable 'query_cache_size'
报错:Unknown system variable 'query_cache_size' mysql 的 java 驱动等级比较低,与mysql 数据库不匹配.
- ftp下载文件失败get: Access failed: 550 Failed to open file. (t1.log)
get: Access failed: 550 Failed to open file. (t1.log) 原因是被SELinux安全访问控制策略限制了. 科普: SELinux(Security-E ...
- jenkins2
创建工程 Eclipse创建工程:注意工程的路径,不是/home/svn,这个是svn的根目录. 是工程上传的路径,Apple直接下面有pom文件. 创建工程,创建一个任务就是创建一个工程. 需要注意 ...
- 今天2.4寸tft触摸屏到手--刷屏驱动小结
2010-04-29 21:28:00 根据给的51程序改成了iccavr,结果改错了2处.导致我找原因找了n久.不过也是一件好事,让我对80i更加熟悉了. 通过protues的逻辑分析仪,找到了问题 ...
- 自学Java第二周的总结
在这一周里我在网上学习了java的对象和类,了解了对象与类以及简单的用法.对象是类的一个实例(对象不是找个女朋友),有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴. ...