Given s1s2s3, 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的更多相关文章

  1. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  2. 40. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  3. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  4. 二维动态规划——Interleaving String

    97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...

  5. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  6. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  7. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  8. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  9. [Swift]LeetCode97. 交错字符串 | Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

随机推荐

  1. ES6学习笔记之数组的扩展

    ✏️1. 扩展运算符 扩展运算符(spread)是三个点(...),将一个数组转为用逗号分隔的参数序列. 普通用法 console.log(...[1,2,3]);//1 2 3 数组拷贝(普通类型深 ...

  2. MySQL用命令行复制表,查看表结构

    一.mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 1 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2; 或 1 CREATE TA ...

  3. Zigbee安全入门(一)—— 技术介绍和安全策略

    么是Zigbee? Zigbee说白了就是类似wifi.蓝牙的一种交换数据的方式,学术点说就是低成本.用于低功耗嵌入式设备(无线电系统),用以促进机器与机器之间高效且有效通信(通常相距10-100米) ...

  4. Hibernate中的Query

    //1:创建query对象,方法里面写hql语句 Query query = session.createQuery("from User"); //2:利用query对象中的方法 ...

  5. 让footer始终待在页面底部

    1.把html和body的height属性设为100%;保证content的高度能撑满浏览器; 2.把#content的高度也设置为100% ,但是这里我们使用了“min-height”属性,而不是的 ...

  6. HTML 和 XHTML 区别

    1.初级改善 为页面添加正确的DOCTYPE 很多设计师和开发者都不知道什么是DOCTYPE,DOCTYPE有什么用. DOCTYPE是document type的简写.主要用来说明你用的XHTML或 ...

  7. python对dataframe的相关用法

    1.选择列名包含特殊字符的列 d2 = d1.loc[:, d1.columns.str.contains('vib')] 2.选择列名开头为包含特殊字符的列 df2 = df1.loc[:, df1 ...

  8. day18 14.连接池介绍

    数据源在软件编程行业有两种概念:一种数据源指的是存储数据的源头(数据库啊文件啊叫数据源),一种指的是连接池(连接池的英文单词叫做DataSource,直译就是数据源).数据源可以指数据库,也可以指连接 ...

  9. day38 10-Spring的Bean的属性的注入

    后处理bean,如果是返回bean,那么什么都不做直接把这个类原封不动地给你返回回去. 在它执行一些逻辑方法的时候对它进行逻辑增强,比如说进行时间监控,权限管理,日志的记录等等. 要做肯定是对正常的类 ...

  10. git之操作准则

    每天下班前合一次代码,每次合代码先pull 不要多人同时修改同一个文件,避免冲突 在每个人自己的分支进行开发,先合并到dev分支解决冲突,确认无冲突后再合并到master