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 = ...
随机推荐
- Luogu P1979 华容道(bfs+最短路)
P1979 华容道 题意 题目描述 小B最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成, 最少需要多少时间. ...
- Docx 生成word文档二
/// <summary> /// 生产word 文档 /// </summary> public class GenerateWord { /// <summary&g ...
- Constructing Roads POJ - 2421 (最小生成树)
思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将 所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...
- 微信小程序 this.setData() 详解
1.定义 setData()函数用于将逻辑层数据发送到视图层,同时对应的改变this.data的值. 2.setData()参数格式 接受一个对象,以键(key)值(value)的方式改变值. 其中, ...
- 2019.9.27 csp-s模拟测试53 反思总结
这个起名方式居然还有后续?! 为什么起名不是连续的?! T1想了半天,搞出来了,结果数组开小[其实是没注意范围].T2概率期望直接跳,后来翻回来写发现自己整个理解错了期望的含义[何].T3错误想到赛道 ...
- Oracle中查看所有的表,用户表,列名,主键,外键
在Oracle中查看所有的表: select * from tab/dba_tables/dba_objects/cat; 看用户建立的表 : select table_name from user_ ...
- linux系统服务
系统服务分类,根据其使用的方法来分,可以被分为三类 a.由init控制的服务:基本都是系统级别的服务,运行级别这一章讲的就是这一类的服务 b.由System V启动脚本启动的服务:和我们打交道最多的一 ...
- Leetcode78. Subsets子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2 ...
- 模拟21 题解(waiting)
留坑待填 效率!!! 题还没改Oh,NO!!!
- Java 分页对象
以前一直没有自己写过分页对象,自己模仿着写了一个分页对象,写完之后感觉也是挺简单的 package com.css.util; import java.io.Serializable;import j ...