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. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  2. intellj远程调试设置

    场景:本地intelllij想远程调试服务器A,服务器A上起得是resin服务 步骤: 1.登陆服务器A,给resin添加启动参数. 方法一:修改resin.xml,修改完后重启resin服务器 在r ...

  3. X-009 FriendlyARM tiny4412 uboot移植之SD Card用起来Kernel boot起来

    <<<<<<<<<<<<<<<<<<<<<<<<< ...

  4. ES6系列汇总

    汇 总 第一节:什么是ES6?新手该如何理解 第二节:ES6新增了let关键字,干嘛用的? 第三节:ES6中另一个不得不说的关键字const 第四节:教你如何快速让浏览器兼容ES6特性 第五节:一个令 ...

  5. git中:关于origin和master

    git的服务器端(remote)端包含多个repository,每个repository可以理解为一个项目.而每个repository下有多个branch."origin"就是指向 ...

  6. Assembly.Load()方法,Assembly.LoadFrom()方法,Assembly.LoadFile()方法的区别!

    参考: http://www.cnblogs.com/benwu/archive/2009/10/24/1589096.html http://www.cnblogs.com/xuefeng1982/ ...

  7. 【资料】wod属性

    各个属性的影响力量 st 影响近战远程伤害和体力体质 co 影响体力(比力量的影响大)智力 in 影响法力和魔法防御灵巧 dx 影响近战远程命中和近战躲闪魅力 ch 影响诅咒和治愈能力,诅咒攻击命中和 ...

  8. ASP.NET MVC:看 MVC 源码,学习:如何将 Area 中的 Controller 放到独立的程序集?

    背景 本文假设您已经熟悉了 ASP.NET MVC 的常规开发方式.执行模型和关键扩展点,这里主要说一下如何使用 ASP.NET MVC 的源代码解决一些问题. 如何将 Area 中的 Control ...

  9. 用C语言实现面向对象的开发

    C语言的对象化模型 面向对象的特征主要包括: .封装,隐藏内部实现 .继承,复用现有代码 .多态,改写对象行为 采用C语言实现的关键是如何运用C语言本身的特性来实现上述面向对象的特征. 1.1 封装 ...

  10. Hive安装与配置——深入浅出学Hive

    第一部分:软件准备与环境规划 Hadoop环境介绍 •Hadoop安装路径 •/home/test/Desktop/hadoop-1.0.0/ •Hadoop 元数据存放目录 •/home/test/ ...