LeetCode之“动态规划”:Interleaving String
题目要求:
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.
这道题的难度还是很大。
在GeeksforGeeks上举了一个例子来说明什么是Interleaving String:
Input: str1 = "AB", str2 = "CD"
Output:
ABCD
ACBD
ACDB
CABD
CADB
CDAB Input: str1 = "AB", str2 = "C"
Output:
ABC
ACB
CAB
具体的对该题的分析和解决参考了一博文:
像这种判断能否按照某种规则来完成求是否或者某个量的题目,很容易会想到用动态规划来实现。
先说说维护量,res[i][j]表示用s1的前i个字符和s2的前j个字符能不能按照规则表示出s3的前i+j个字符,如此最后结果就是res[s1.length()][s2.length()],判断是否为真即可。接下来就是递推式了,假设知道res[i][j]之前的所有历史信息,我们怎么得到res[i][j]。可以看出,其实只有两种方式来递推,一种是选取s1的字符作为s3新加进来的字符,另一种是选s2的字符作为新进字符。而要看看能不能选取,就是判断s1(s2)的第i(j)个字符是否与s3的i+j个字符相等。如果可以选取并且对应的res[i-1][j](res[i][j-1])也为真,就说明s3的i+j个字符可以被表示。这两种情况只要有一种成立,就说明res[i][j]为真,是一个或的关系。所以递推式可以表示成
res[i][j] = res[i-][j]&&s1.charAt(i-)==s3.charAt(i+j-) || res[i][j-]&&s2.charAt(j-)==s3.charAt(i+j-)
时间上因为是一个二维动态规划,所以复杂度是O(m*n),m和n分别是s1和s2的长度。最后就是空间花费,可以看出递推式中只需要用到上一行的信息,所以我们只需要一个一维数组就可以完成历史信息的维护,为了更加优化,我们把短的字符串放在内层循环,这样就可以只需要短字符串的长度即可,所以复杂度是O(min(m,n))。
具体程序如下:
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int szS1 = s1.size();
int szS2 = s2.size();
int szS3 = s3.size();
if(szS3 != szS1 + szS2)
return false;
if(szS1 == && szS2 == )
return s3 == s1 || s3 == s2;
string minWord = szS1 > szS2 ? s2 : s1;
string maxWord = szS1 > szS2 ? s1 : s2;
int szMinWord = min(szS1, szS2);
int szMaxWord = max(szS1, szS2);
vector<bool> match(szMinWord + , true);
for(int i = ; i < szMinWord + ; i++)
{
match[i] = match[i - ] && minWord[i - ] == s3[i - ];
}
for(int i = ; i < szMaxWord; i++)
{
match[] = match[] && s3[i] == maxWord[i];
for(int j = ; j < szMinWord + ; j++)
{
match[j] = (match[j - ] && minWord[j - ] == s3[i + j]) || (match[j] && maxWord[i] == s3[i + j]);
}
}
return match[szMinWord];
}
};
LeetCode之“动态规划”:Interleaving String的更多相关文章
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 二维动态规划——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
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- LeetCode解题报告—— Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 【leetcode】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 StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- leetcode笔记 动态规划在字符串匹配中的应用
目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...
随机推荐
- 20 ViewPager demo5,6:FragmentAdapter 导航数据
Demo5 文件结构: MainActivity.java package com.qf.day20_viewpager_demo5; import java.util.ArrayList; impo ...
- Ejb远程调用-jboss服务器调用服务器-Bean调用Bean
英文参考地址 https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+o ...
- OpenCV相机标定
标签(空格分隔): Opencv 相机标定是图像处理的基础,虽然相机使用的是小孔成像模型,但是由于小孔的透光非常有限,所以需要使用透镜聚焦足够多的光线.在使用的过程中,需要知道相机的焦距.成像中心以及 ...
- MySQL设计软件登录模块
学了一段时间的Java了,思量着做一点简单的小模块的东西吧,于是就有了下面的这个简单的小案例. 大致实现的功能就是注册于登录还有就是用到了一点,分层思想.仅此而已,所以非常的适合新手围观. 建立好数据 ...
- 安卓服务Service详解
service(服务)是安卓中的四大组件之一,它通常用作在后台处理耗时的逻辑,与Activity一样,它存在自己的生命周期,也需要在清单文件中配置相关信息,本博客将对Service的各个知识点进行详细 ...
- 【Unity Shaders】使用Unity Render Textures实现画面特效——建立画面特效脚本系统
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- LaTex计数器
记数器 绝大多数都与可以改变他们的命令有相同的名称 part chapter section subsection paragraph subparagraph page equation figur ...
- Tom DeMarco:软件工程这个概念已过时?
原文作者:Tom Demarco,写于2009年7月 作者简介:Tom DeMarco是大西洋系统协会(www.atlsysguild.com)的负责人.他的职业生涯开始于贝尔实验室,是结构化分析和设 ...
- 为什么会存在using filesort
当使用explain分析SQL时常常会遇到extra的其中一值为using filesort,如: PRIMARY KEY (`id`), KEY `uid` (`uid`) explain se ...
- Cocos2D:塔防游戏制作之旅(一)
原文地址:http://www.raywenderlich.com/37701/how-to-make-a-tower-defense-game-tutorial 由Pablo Ruiz写的入门教程, ...