动态规划之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 = ...
随机推荐
- Linux 重启nginx
重启 1.验证nginx配置文件是否正确 方法一:进入nginx安装目录sbin下,输入命令./nginx -t 看到如下显示nginx.conf syntax is ok nginx.conf te ...
- 第四章 CSS3概述
1.CSS3新增常用选择器(1)结构性伪类选择器:root 文档根元素 :nth-child(n) 第N个子元素"first-child 第一个元素 :kast-child 最后一个子元素 ...
- Other Problems
http://www.cnblogs.com/coder211/p/7919749.html http://blog.jobbole.com/17763/http://www.open-open.co ...
- python2.7之乱码问题
python 3之后当然不存在乱码问题了.python 2的乱码问题有时就有点头疼了.(代码均为在windows下测试) 示例:保存为test1.py 报错信息如下: 解决办法: 我将代码保存为tes ...
- html5-label标签
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Java多线程-----线程池详解
1. 线程池的实现原理 提交一个任务到线程池中,线程池的处理流程如下: 判断线程池里的核心线程是否都在执行任务,如果不是(核心线程空闲或者还有核心线程没有被创建)则创建一个新的工作线程来执行任务.如果 ...
- jfinal 项目 控制层、ORM层、AOP层,在发表之前一定要记得保存
一.ORM简介 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.简单的说,ORM是通过使用描述对象和数据 ...
- python 文件描述符
先上一张图 文件描述符是内核为了高效管理已经被打开的文件所创建的索引, ----非负整数 ----用于指代被打开的文件 ----所有执行i/o操作的系统调用都是通过文件描述符完成的 进程通过文件描述符 ...
- springboot报错Whitelabel Error Page
第一次使用springboot没有问题.隔了两天继续看.一直报错Whitelabel Error Page. 重新搭建试了任何方法都错了. 报的就是一个404错误,犯了一个习惯性错误,一般都是loca ...
- Hive批量删除历史分区
批量删除历史分区和数据可以采用如下操作: -- 删除20180101之前的所有分区 alter table example_table_name drop if exists partition (d ...