【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"," ...
随机推荐
- SqlServer代理执行[分发清除: distribution] 无法删除快照文件
每天偶尔检查数据库作业是否失败,发现有错误 select top 10 job_id,run_date,run_time,run_duration,step_name,message from ms ...
- ThinkPHP3.2.3 安装教程
本文以 Windows 平台为例 安装前准备:Windows操作系统的电脑,php编程环境(配置好了Apache.MySql.php).推荐wampserver. 待安 ...
- 【bzoj3573】[HNOI2014]米特运输
题目描述 米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题.D星上有N个城市,我们将其顺序编号为1到N,1号城市为首都.这N个城 ...
- 7-RandomAccessFile 随机流
package com.io; import java.io.File; import java.io.FileNotFoundException; import java.io.IOExceptio ...
- 虚拟机安装Ubuntu三种网络模式
VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). NAT(网络地址转换) 在NAT模式下,虚拟系统需要借助NAT(网络地址转换)功能,通过宿主 ...
- HDU 5073 Galaxy(2014鞍山赛区现场赛D题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...
- CoreGraphics QuartzCore CGContextTranslateCTM 用法
原点是: 左下角 旋转: 逆时针 位移: 右上为正, 左下为负 CGContextTranslateCTM CGContextRotateCTM CGContextScaleCTM 而且, 以上几 ...
- cocos2d界面渲染
渲染是visit函数来做的, visit是先将不可见的节点和他所有的子节点都跳过, 然后再看节点的子节点是否为空, 如果为空的话直接看这个节点是否在摄像机可见范围之内, 如果在就渲染这个节点, 否则什 ...
- Qt5 任务栏托盘功能实现
23333 有一阵子没写博客了,研究了挺长时间qt,学到任务栏托盘时简直无语,网上找得到的代码大多是废码,Qt5不支持或者本身就有毛病不能实现却被n多人转来转去的,甚是无语. 简单托盘功能以下在Qt5 ...
- phpcms的评论改为留言板研究
研究背景: phpcms里面默认是没有留言板的,之前我的博客里发过一个二次开发简介,里面有一个简单的留言板,包含前台提供表单,后台留言审核等功能,但是不提供用户登录等操作. 研究思路: phpcms里 ...