Interleaving String

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.

可以用递归做,每匹配s1或者s2中任意一个就递归下去。但是会超时。

因此考虑用动态规划做。

s1, s2只有两个字符串,因此可以展平为一个二维地图,判断是否能从左上角走到右下角。

当s1到达第i个元素,s2到达第j个元素:

地图上往右一步就是s2[j-1]匹配s3[i+j-1]。

地图上往下一步就是s1[i-1]匹配s3[i+j-1]。

示例:s1="aa",s2="ab",s3="aaba"。标1的为可行。最终返回右下角。

0  a  b

0   1  1  0

a   1  1  1

a   1  0  1

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int m = s1.size();
int n = s2.size();
if(m+n != s3.size())
return false;
vector<vector<bool> > path(m+, vector<bool>(n+, false));
for(int i = ; i < m+; i ++)
{
for(int j = ; j < n+; j ++)
{
if(i == && j == )
// start
path[i][j] = true;
else if(i == )
path[i][j] = path[i][j-] & (s2[j-]==s3[j-]);
else if(j == )
path[i][j] = path[i-][j] & (s1[i-]==s3[i-]);
else
path[i][j] = (path[i][j-] & (s2[j-]==s3[i+j-])) || (path[i-][j] & (s1[i-]==s3[i+j-]));
}
}
return path[m][n];
}
};

【LeetCode】97. Interleaving String的更多相关文章

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

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

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  5. 【原创】leetCodeOj --- Interleaving String 解题报告

    题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3  ...

  6. 【Lintcode】029.Interleaving String

    题目: Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2 ...

  7. 【LeetCode】344. Reverse String 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...

  8. 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

随机推荐

  1. Xcode常用插件推荐

    1.Xcode插件管理工具–Alcatraz的安装 Alcatraz是针对Xcode的一款插件管理器,通过Alcatraz可以非常方便的管理插件,包括安装.删除.升级等操作. 官方网站 安装方法一(推 ...

  2. 一个iframe注入漏洞,也是微软的 Application["error"] 漏洞

    最近学校进行安全等级评估,有人给我打电话,说我之前写的一个网站存在iframe注入漏洞,页面是error页面.我于是用netsparker扫描了自己的网站,果然发现error页面存在漏洞,我写网站的时 ...

  3. Regulator IC forms convenient overvoltage detector

    Figure 1 shows a simple, stand-alone overvoltage detector. The intent of the circuit is to monitor a ...

  4. windows系统上安装与使用Android NDK r8d(二)

    四.    在eclipse中集成c/c++开发环境    1. 装Eclipse的C/C++环境插件:CDT,这里选择在线安装.          首先登录http://www.eclipse.or ...

  5. Linux系统下查看目录大小

    转载:http://blog.csdn.net/iamlaosong/article/details/7085178 我们有个系统每天要下载数据,为了检查下载数据是否正常,需要查看下载数据目录大小,因 ...

  6. pytest文档12-skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试. 常见示例是 ...

  7. 将Maven2项目转为MyEclipse项目

    现在项目中,大家开始用jetty.它不用像在MyEclipse中使用Tomcat那样要部署,也不用像在Tomcat中那样,要把应用都放到webapp文件夹下.jetty可以直接用你的项目的目录结构. ...

  8. poj 1995 Raising Modulo Numbers 题解

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6347   Accepted: ...

  9. java中常量定义在interface中好还是定义在class中

    Java中interface中定义变量都是"public static final" 类型的, 也就是常量, 因此很多人在interface定义常用的常量,除此之外单独定义一个cl ...

  10. C#对.zip 存档读取和写入【转】

    Framework4.5支持 引用: System.IO.Compression.dll,System.IO.Compression.FileSystem.dll 提取压缩文件 ZipFile.Ext ...