LeetCode-Interleaving String[dp]
Interleaving String
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.
标签: Dynamic Programming String
分析:动态规划;设boolean数组dp[i][j]表示字符串s1[0...i-1]和字符串s2[0...j-1]是否可以匹配到字符串s3[0...i+j-1],
因此有字符s3[i+j-1]可以由字符s1[i-1]或字符s2[j-1]来匹配,所以:
如果s1[i-1]==s3[i+j-1],则dp[i][j]=dp[i-1][j];
如果s2[j-1]==s3[i+j-1],则dp[i][j]=dp[i][j-1];
所以状态转移方程为:dp[i][j]=(dp[i-1][j]&&s1[i-1]==s3[i+j-1])||(dp[i][j-1]&&s2[j-1]==s3[i+j-1])
参考代码:
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
int len1=s1.length();
int len2=s2.length();
int len3=s3.length();
if(len1+len2!=len3)
return false;
boolean dp[][]=new boolean[len1+1][len2+1];
dp[0][0]=true;
for(int j=1;j<=len2;j++){
dp[0][j]=dp[0][j-1]&&s2.charAt(j-1)==s3.charAt(j-1);
}
for(int i=1;i<=len1;i++){
dp[i][0]=dp[i-1][0]&&s1.charAt(i-1)==s3.charAt(i-1);
}
for(int i=1;i<=len1;i++){
for(int j=1;j<=len2;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[len1][len2];
}
}
LeetCode-Interleaving String[dp]的更多相关文章
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Interleaving String 解题思路
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- [leetcode]Interleaving String @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- Interleaving String (DP)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given:s1 ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
随机推荐
- Copy_on_write的简单实现
Copy_on_write即写时复制,它的原理是通过引用计数来实现的. 即在分配空间时多分配额外的空间,用来记录有多少个指针指向该空间.当有新的指针指向该空间,引用计数则加一,当要释放该空间时,引用计 ...
- tomcat之 JDK8.0安装、tomcat-8.5.15安装
前言:JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK. JDK是整个Ja ...
- JAVA web.xml中引用多个XML
web.xml里加<context-param><param-name>contextConfigLocation</param-name><param-va ...
- SQL Server数据类型有哪些
一. 整数数据类型 整数数据类型是最常用的数据类型之一. 1.INT (INTEGER) INT (或INTEGER)数据类型存储从-2的31次方 (-2 ,147 ,483 ,648) 到2的31次 ...
- JavaScript事件(二)
例题顺序: 1.子菜单下拉2.图片轮播3.选项卡效果4.进度条制作5.滑动效果6.滚动固定效果 1.子菜单下拉 <!DOCTYPE html PUBLIC "-//W3C//DTD X ...
- 在linux系统下安装redis
去官网找到合适的版本,可以直接下载下来,再用fxp上传,也可以直接以下面这种方式下载:$ wget http://download.redis.io/releases/redis-3.2.9.tar. ...
- OpenCV探索之路(十七):Mat和IplImage访问每个像素的方法总结
在opencv的编程中,遍历访问图像元素是经常遇到的操作,掌握其方法非常重要,无论是Mat类的像素访问,还是IplImage结构体的访问的方法,都必须扎实掌握,毕竟,图像处理本质上就是对像素的各种操作 ...
- Angular4 后台管理系统搭建(2) - flexgrid 单元格模板 wjFlexGridCellTemplate 的坑
这几天中了很多坑,尤其是两个大坑.先是运行环境的坑,在是flexgrid单元格内部模板的坑.这里记录下. 一开始我遇见一些很奇怪的问题,按网上的说法,别人这么写代码都正常,就在我机器上不正常.按以前的 ...
- leetcode-486-Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- 【Android Developers Training】 92. 序言:使用同步适配器传输数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...