【leetcode】 Interleaving String (hard)
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.
思路:做过几道动态规划了,终于也有了点感觉。一次就AC了,好开心啊~
用dp[m][n]来存储s3[0 ~ m+n-1]是否是s1[0~m-1]与s2[0~n-1]交替组成的。注意,没有必要存s3的维度,因为s1[0~m-1]与s2[0~n-1]只能匹配s3的m+n个字符,即第三个维度是确定的。
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]
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
if(len3 != len1 + len2)
return false;
vector<vector<bool>> dp(len1 + , vector<bool>(len2 + , false));
dp[][] = true;
for(int i = ; i < len1 + ; i++)
{
dp[i][] = dp[i-][] && (s1[i-] == s3[i-]);
}
for(int j = ; j < len2 + ; j++)
{
dp[][j] = dp[][j-] && (s2[j-] == s3[j-]);
}
for(int i = ; i < len1 + ; i++)
{
for(int j = ; j < len2 + ; j++)
{
dp[i][j] = (dp[i-][j] && (s1[i-] == s3[i+j-]))
|| (dp[i][j-] && (s2[j-] == s3[i+j-]));
}
}
return dp[len1][len2];
}
};
【leetcode】 Interleaving String (hard)的更多相关文章
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】984. String Without AAA or BBB 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【leetcode】 Scramble String (hard)★
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
随机推荐
- Dirty Markup - 在线代码美化工具
如果你需要一个帮助你规整书写混乱的代码的工具的话,我强烈推荐给你这个在线代码美化工具 - Dirty Markup.这个在线工具能够帮助你有效的处理HTML/HTML5,CSS和javascript代 ...
- R-数据导入
目录 键盘输入 导入文本文件 导入Excel文件 访问数据库管理系统 键盘输入 > mydata <- data.frame(age=numeric(0), gender=characte ...
- AlwaysOn可用性组功能测试(二)--SQL Server群集故障转移对AlwaysOn可用性组的影响
三. SQL Server群集故障转移对AlwaysOn可用性组的影响 1. 主副本在SQL Server群集CLUSTEST03/CLUSTEST03上 1.1将节点转移Server02.以下是故障 ...
- Max Degree of Parallelism最大并行度配置
由于公司的业务在急速增长中,发现数据库服务器已经基本撑不住这么多并发.一方面,要求开发人员调整并发架构,利用缓存减少查询.一方面从数据库方面改善并发.数据库的并行度可设置如下: 1)cost thre ...
- Access应用日志<一>
今天在确认实习生不能帮忙搭建数据库后,自己根据业务需求尝试搭了一个小型access数据库. 主要目的:储存历史月度数据,避免每次从公司数据库下载数据的麻烦,节省数据拉取时间. 搭建了以acct id为 ...
- android版微信5.2.1更新 支持微信聊天记录备份到电脑上
昨天微信 5.2.1 for Android 全新发布了,和微信 5.2.1 for iPhone一样,支持拍照分享,可以把照片发送给多个朋友,最重要的一个更新是支持微信聊天记录备份到电脑(可以通过腾 ...
- 【codevs1380】没有上司的舞会
题目描述 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会,要求与会职员的快乐指数 ...
- Swift2.1 语法指南——协议
原档: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programm ...
- 第24天 runtime
面试时被问到一个问题,如何实现weak变量的自动置nil?当时也不知道. 今天在实现target-action模式时,如何调用SEL,刚开始只会PerformSelector,但不能传递多个参数,后来 ...
- Linux文件处理命令
1.权限处理 1.1 方法一 使用+-=的方法 1.1.1权限 rwx r 读 w 写 x 执行 1.1.2用户 ugoa u 所有者 g 用户组 o 其他人 a 表示以上所有 修改文件的方法 例: ...