动态规划之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 = ...
随机推荐
- 实现多线程异步自动上传本地文件到 Amazon S3
最近抽空做个小工具,使用AWSSDK 对本地文件目录监控,并自动同步上传文件到S3 的过程,使用的是多线程异步上传,针对大文件进行了分块 参考文献: https://www.codeproject.c ...
- MFC 显示图片
//定义成员变量 CStatic m_picture; m_picture.Create(L"XXX",WS_VISIBLE|WS_CHILD|SS_BITMAP ,CRect(, ...
- QT 添加 lib库
扒自网友文章: 一.添加第三方的头文件 首先,添加头文件 #include "ControlCAN.h" 然后,再将这个头文件放到工程的目录下 二.添加.lib文件 首先,将.l ...
- CSU 1838 Water Pump(单调栈)
Water Pump [题目链接]Water Pump [题目类型]单调栈 &题解: 这题可以枚举缺口,共n-1个,之后把前缀面积和后缀面积用O(n)打一下表,最后总面积减去前缀的i个和后缀的 ...
- composer----------composer初体验,如何安装,如何下载
最近PHP里面比较火的一款框架laravel,想学一下看下这个框架到底哪里好.这款框架的中文官网激励推荐composer,没办法就去学了一些composer.结果整了半天,还不如看一段短视频学的容易. ...
- 同一个电脑安装两个jdk版本
同一个电脑安装两个jdk版本 场景:公司项目使用的jdk为1.,最近不是很忙,学习scala.该系统使用到了jdk1.8的特性,所以I need 俩版本,开整!!! . 准备两个版本的jdk我的两个j ...
- TP图片上传
//控制器文件 public function index(){ if(!empty($_POST)){ $file = $_FILES["file"]; if(!isset($f ...
- sqlserver Distributed Transaction 分布式事务
在webapi+ef+sqlserver开发项目时,利用transcope实现应用层级的事务时,偶尔会报分布式事务错误,而且很而复现,特别蛋疼.现将自己的解决方法初步整理下. 分析原因:搭建repos ...
- netCore webapi Uow实现方式
参照 http://www.cnblogs.com/GreedyL/p/7474368.html 思路: 1.创建ActionFilter拦截请求,在拦截器中注入IUOW,IUOW里面注入IDbCon ...
- BR(BoomerangRobot)机器人项目
项目宗旨:推动机器人技术及相关知识的普及,增进广大机器人DIYer们的交流,提高爱好者们自身的专业水平,项目提供以机器人BR(boomerangrobot)为硬件平台,ROS(robot operat ...