【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 ...
随机推荐
- [HTML]不让TD中的文本撑开表格(转)
我经常会遇到这样的问题:在设计好宽度的表格中,因为有些文本信息过长,而把表格撑开(弄的面目全非)!很让人头疼.其实解决这个问题很简单,只要在<td>的样式中加入word-break: br ...
- 使用Win2D在UWP程序中2D绘图(一)
在新的Windows UWP程序中,引入了一个新的API库: Win2D.它是一个d2d的封装,可以直接使用C#来快速实现高效2D绘图了.这个API虽然在Win8.1时代就开始着手开发了,但最近才完善 ...
- 利用Lucene与Nutch构建简单的全文搜索引擎
文章地址 1.简介 本次实现分为两个部分,第一个部分是利用Lucene构建一个全文的搜索引擎,另外一部分则是利用Nutch实现同样的功能.由于Lucene并不是一个可以直接运行的程序,且不具备爬虫和文 ...
- MySql_SQLyog+SQL Assistant实现智能提示
相信用过sqlserver+SQL Assistant的同学都知道其智能提示多么方便,但是转到mysql后,无论是使用navicat还是webbench都无法实现较好的智能提示效果, 最终在网上找到使 ...
- 2013年,移动App设计的13大精髓
摘要:在 过去的一年里,移动成主流也让众多的移动应用如雨后春笋般层出不穷,在众多开发者从中获利的同时竞争也愈演愈烈,如何才能保证自己立于不败之地?用户是上 帝,一切还得从应用说起.本文总结了新一年里A ...
- poj 3258 River Hopscotch 题解
[题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...
- iOS:网络编程解析协议一:HTTP超文本传输协议
HTTP传输数据有四种方式:Get方式.Post方式.同步请求方式.异步请求方式.具体的介绍,前面已经有过系统的讲解,这次主要进行具体的举. 说明:同步和异步请求方式在创建链接对象和创建请求对象时,用 ...
- C++中函数调用时的三种参数传递方式详解
在C++中,参数传递的方式是“实虚结合”. 按值传递(pass by value) 地址传递(pass by pointer) 引用传递(pass by reference) 按值传递的过程为:首先计 ...
- Configure Trusted Roots and Disallowed Certificates
Configure Trusted Roots and Disallowed Certificates Updated: May 5, 2014 Applies To: Windows 8.1, Wi ...
- [Functional Programming] Write a simple version of Maybe
Maybe has two types: Just / Nothing. Just() will just return the value that passed in. Nothing retur ...