[LeetCode] Interleaving String [30]
题目
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.
解题思路
交差字符串。给3个字符串s1, s2, s3,推断s3是不是由s1和s2组成的交叉字符串。
设s1长度为m, s2长度为n,推断 s3[0...m+n-1] 是不是由s1[0...m-1], s2[0...n-1]组成的交叉字符串,如果s1[m-1] == s3[m+n-1],则仅仅需推断s3[0...m+n-2]是不是由s1[0...m-2], s2[0...n-1]组成的交叉字符串...,依次这样推断下去。从这能够总结,这个问题能够划分为比它小的问题,这里使用动态规划应该比較合适。
dp[i][j]:表示s3[i+j-1] 是不是 由s1[0...i-1], s2[0...j-1]组成的交叉字符串。
dp[i][j] = dp[i][j] || dp[i-1][j] ; (s1[i-1]==s3[i+j-1])
dp[i][j] || dp[i][j-1] ; (s2[j-1]==s3[i+j-1])
代码实现
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int m = s1.size();
int n = s2.size();
if(n+m != s3.size()) return false;
vector<vector<bool> > dp(m+1, vector<bool>(n+1, false));
//初始化dp[i][0]
for(int i=0;i<m; ++i){
if(s1[i] == s3[i])
dp[i+1][0] = true;
}
//初始化dp[0][i]
for(int i=0; i<n; ++i){
if(s2[i] == s3[i])
dp[0][i+1] = true;
}
dp[0][0] = true;
int k;
for(int i=1; i<=m; ++i)
for(int j=1; j<=n; ++j){
k = i+j;
if(s1[i-1] == s3[k-1])
dp[i][j] = dp[i][j] || dp[i-1][j];
if(s2[j-1] == s3[k-1])
dp[i][j] = dp[i][j] || dp[i][j-1];
}
return dp[m][n];
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Interleaving String [30]的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【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 ...
随机推荐
- UDP通讯协议
常见的通讯协议有udp和tcp. 先来简单了解一下这两个协议各自的特点: UDP: --将数据及源.目的封装在数据包中,不需要建立连接: --每个数据包的大小限制在64k以内: --因无连接,是不可靠 ...
- MapReduce源代码浅析
Thanks @读程序的手艺人 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVvemhhbmZlbmc=/font/5a6L5L2T/fontsize ...
- LDA-线性判别分析(三)
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- [Immutable,js] Immutable.Record() as data models
The Immutable.js Record() allows you to model your immutable data much like you would model data wit ...
- for-in用法
var nyc = { fullName: "New York City", mayor: "Bill de Blasio", popu ...
- Http请求和响应报文基础知识
一 HTTP请求报文(http://en.wikipedia.org/wiki/List_of_HTTP_header_fields) 请求报文由三部分组成:请求行,请求头和请求体. 请求行:请求方法 ...
- vs2012 boost配置
1.去www.boost.org下载最新的boost,我下载了boost_1_60_02.(我放在D:/cpp目录下)解压到当前文件夹3.打开VS2012->VS TOOLS->VS命令提 ...
- eclipse中Java代码导入包,出现main.java前缀
1.工程右击,按照下图指示的选择 2.按照下图,进行删除选中项,然后点击OK保存
- IOS 手机端搜索硬件设备 --- 物联网
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #i ...
- .Net Memory -- GC基本知识
参考资料: http://blogs.msdn.com/b/tess/archive/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of ...