【LeetCode】97. Interleaving String
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.
可以用递归做,每匹配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的更多相关文章
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【原创】leetCodeOj --- Interleaving String 解题报告
题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3 ...
- 【Lintcode】029.Interleaving String
题目: Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2 ...
- 【LeetCode】344. Reverse String 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
随机推荐
- memcached添加日志输出
引子:qa的memcached总是隔一段时间挂掉,导致qa进不去.决定查一下原因,于是添加日志输出,等下次出错便于查阅.定位问题. memcache默认没有日志输出.如果想把memecache服务日志 ...
- md5加密,md5加盐加密和解密
package com.java.test; import java.security.MessageDigest; import java.security.SecureRandom; import ...
- Linux下使用SSH远程执行命令方法收集
说明:可以做SSH免密登录之后执行,这样可以省去每次执行输入密码的提示. 对于简单的命令: 如果是简单执行几个命令,则: ssh user@remoteNode "cd /home ; ls ...
- python中获取当前位置所在的行号和函数名(转)
http://www.vimer.cn/2010/12/%E5%9C%A8python%E4%B8%AD%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E4%BD%8D%E7 ...
- linux shell 正则表达式(BREs,EREs,PREs)差异比较(转,当作资料查)
转载: 在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或 替换那些符合某个模式的文本内容.许多程序设计语 ...
- JMeter技巧集锦
收藏些介绍JMeter使用知识的文章 1.JMeter技巧集锦 http://www.javaworld.com/javaworld/jw-07-2005/jw-0711-jmeter.html (网 ...
- ODS与数据仓库
数据仓库是目前主要的数据存储体系.数据仓库之增W.H.Inmon认为,数据仓库是指支持管理决策过程的.面向主题的.集成的.随时间而变的.持久的数据的集合.简单地说,一个数据仓库就一个自数据库的商业应用 ...
- CSDN日报20170328——《你看那个人他像一条狗》
[程序人生]你看那个人他像一条狗 作者:清纯的微笑 今年三十了,到了传说中程序猿最应该迷茫的年龄了,那么我迷茫吗,没的说,依照华为34岁就要劝退的要求,我还有4年的程序生涯. [微信小程序]重磅!个人 ...
- OpenCV学习(4) Mat的基本操作(1)
图像在OpenCV中都是通过Mat类来存储的,Mat可以用来表示N维矩阵,当然用的最多的还是二维矩阵. Mat类有两部分组成:第一部分是头信息,这些信息主要用来描述矩阵,比如矩 ...
- Orchard运用 - 定制呈现最新博客文章
每个博客系统为了吸引更多访问量,一般都会在首页或侧边栏列举一些最新文章/随笔以获取更多点击.其实也就是查询出最新的几篇文章并按照简练的方式呈现,比如一般都只有标题及其对应的链接,有时也会标注一下作者和 ...