LeetCode97 Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. (Hard)
For example,
Given:
s1 = "aabcc",
s2 = "dbbca",
When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.
分析:
开始的思路是DFS搜索,当然结果也是华丽地超时了。
然后考虑动态规划。开始定义状态上出现了一些问题,甚至想到三维数组。
但是仔细考虑一下,采用双序列动态规划问题常见的状态定义,dp[i][j]表示s1的前 i 个字符和s2的前 j 个字符能否搭配组成s3(自然就是前i + j 位)。
然后就是递推关系式根据s1[i - 1] 与 s2[j - 1]与 s3[i + j - 1]是否相等(具体见代码)
注意:
s1,s2的长度加起来不等于s3的长度,直接返回false
初始化的时候发现二维数组直接用{false}并不能全部初始化,这里WA了一次
代码:
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if (s1.size() + s2.size() != s3.size()) { //长度不一致判断
return false;
}
bool dp[s1.size() + ][s2.size() + ]; //初始化不能做到一次初始化问false
dp[][] = true;
for (int i = ; i <= s1.size(); ++i) {
if (s1[i - ] == s3[i - ] && dp[i - ][]) {
dp[i][] = true;
}
else {
dp[i][] = false;;
}
}
for (int i = ; i <= s2.size(); ++i) {
if (s2[i - ] == s3[i - ] && dp[][i - ]) {
dp[][i] = true;
}
else {
dp[][i] = false;
}
}
for (int i = ; i <= s1.size(); ++i) {
for (int j = ; j <= s2.size(); ++j) {
dp[i][j] = false;
if (s1[i - ] == s3[i + j - ] && s2[j - ] && s3[i + j - ]) {
dp[i][j] = (dp[i - ][j] || dp[i][j - ]);
}
else if (s1[i - ] == s3[i + j - ]) {
dp[i][j] = dp[i - ][j];
}
else if (s2[j - ] == s3[i + j - ]) {
dp[i][j] = dp[i][j - ];
}
}
}
return dp[s1.size()][s2.size()];
}
};
LeetCode97 Interleaving String的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 二维动态规划——Interleaving String
97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- [Swift]LeetCode97. 交错字符串 | Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
随机推荐
- leetcode 448 - 476
448. Find All Numbers Disappeared in an Array Input: [4,3,2,7,8,2,3,1] Output: [5,6] 思路:把数组的内容和index ...
- uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤
uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤 这里以vant-weapp为例 uni-app官方文档介绍引入组件的方法 1. 新建相关目录 根目录下创建 wxcomponen ...
- 移动web--移动屏幕适配-完整的viewport设置
- Iterm2 快捷键介绍
Mac 原来自带的终端工具 Terminal 不好用是出了名的,虽然最近几个版本苹果稍微做了些优化,功能上,可用性方面增强不少,无奈有个更好用的 Iterm2 摆在那,基本上也就没有多少出场机会了 I ...
- IE9没有内置鼠标手势,还要自己写
写了个IE插件,然后获取鼠标,信息, 模拟了鼠标手势,在虚拟机里面测试,完全好使,但是现在又不敢在Win7上用了. 愁死了... 为了实现一个鼠标手势. 写的那破玩意,竟然50多K.....太大了.. ...
- 【洛谷】P1590 失踪的7
P1590 失踪的7 题目描述 远古的Pascal人也使用阿拉伯数字来进行计数,但是他们又不喜欢使用7,因为他们认为7是一个不吉祥的数字,所以Pascal数字8其实表示的是自然数中的7,18表示的是自 ...
- PhpSpreadsheet处理表格2
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- Codeforces 356A
这题有个注意的地方,就是对集合边读边删除的时候,应该尤为注意.. my_set.erase(it++) #include <iostream> #include <cstring ...
- freemarker 取值(插值)(转)
Java数据模型 1)基本数据类型取值 八种基本的java类型:byte.short.int.long:float,double:char:boolean 对应的封装类型:Byte.Short.Int ...
- phpinfo空白
<?php phpinfo(); ?> 以上代码放入一个kk.php的文件里,但浏览器显示是空白. 原因: 检查php.ini文件里 disable_functions =后面有没有限制p ...