题目链接

  题目要求: 

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

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

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

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

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

  3. 【LeetCode】97. Interleaving String

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

  4. LeetCode解题报告—— Interleaving String

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

  5. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

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

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

  7. 【leetcode】Interleaving String

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

  8. Leetcode:Interleaving String 解题报告

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

  9. leetcode笔记 动态规划在字符串匹配中的应用

    目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...

随机推荐

  1. SQL Server 扩展事件(Extented Events)从入门到进阶(2)——在GUI中创建基础扩展事件

    本文属于 SQL Server 扩展事件(Extented Events)从入门到进阶 系列 第一篇文章中提到了如何在Profiler中创建跟踪(trace),并以服务器端(server-side)跟 ...

  2. Python 一个奇特的引用设定

    def f(x): print 'original' if x > 0: return f(x-1) return 0 g = f def f(x): print 'new' return x ...

  3. cassandra 监控方案评估

    摘要 最开始做cassandra monitor 方案的选型时,主要是从cassandra 本身入手,后来发现cassandra运行在JVM上,所有的metrics都是通过JMX 暴露出来.所以又可以 ...

  4. android RecycleView Adapter简单封装

    早些时候我们使用系统提供个的BaseAdapter的时候为了满足大家的需要,我们总会对BaseAdapter做一层上层的封装,然后对于实际业务我们只需要关心getView里面的View即可,是代码可读 ...

  5. UNIX网络编程——非阻塞connect

    当在一个非阻塞的TCP套接字上调用connect时,connect将立即返回一个EINPROGRESS错误,不过已经发起的TCP三次握手继续进行.我们接着使用select检测这个连接或成功或失败的已建 ...

  6. Android开发学习之路--MAC下Android Studio开发环境搭建

    自从毕业开始到现在还没有系统地学习android应用的开发,之前一直都是做些底层的驱动,以及linux上的c开发.虽然写过几个简单的app,也对android4.0.3的源代码做过部分的分析,也算入门 ...

  7. Handler,MessageQueue Loop 和Message的原理解析

    先介绍和handler一起工作的几个组件 Handler的方法介绍 代码示例 package liu.peng.weather; import java.util.Timer; import java ...

  8. 【Unity Shaders】Lighting Models —— 光照模型之Lit Sphere

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  9. Uva - 400 - Unix ls

    先计算出最长文件的长度M,然后计算列数和行数,最后输出即可. AC代码: #include <iostream> #include <cstdio> #include < ...

  10. 《C语言点滴》书评

    说起C语言方面的书,你最先想到的是哪一本?不论图书本身是好是坏,反正我想到的是谭浩强的<C程序设计>--它已然是一部"圣经"了.那么,为什么赵岩老师还要写一本<C ...