题目

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.

原题链接(点我)

解题思路

交差字符串。给3个字符串s1, s2, s3,推断s3是不是由s1和s2组成的交叉字符串。

设s1长度为m, s2长度为n,推断 s3[0...m+n-1] 是不是由s1[0...m-1], s2[0...n-1]组成的交叉字符串,如果s1[m-1] == s3[m+n-1],则仅仅需推断s3[0...m+n-2]是不是由s1[0...m-2], s2[0...n-1]组成的交叉字符串...,依次这样推断下去。从这能够总结,这个问题能够划分为比它小的问题,这里使用动态规划应该比較合适。

dp[i][j]:表示s3[i+j-1] 是不是 由s1[0...i-1], s2[0...j-1]组成的交叉字符串。

dp[i][j] = dp[i][j] || dp[i-1][j] ; (s1[i-1]==s3[i+j-1])

              dp[i][j] || dp[i][j-1] ; (s2[j-1]==s3[i+j-1])

代码实现

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int m = s1.size();
int n = s2.size();
if(n+m != s3.size()) return false;
vector<vector<bool> > dp(m+1, vector<bool>(n+1, false));
//初始化dp[i][0]
for(int i=0;i<m; ++i){
if(s1[i] == s3[i])
dp[i+1][0] = true;
}
//初始化dp[0][i]
for(int i=0; i<n; ++i){
if(s2[i] == s3[i])
dp[0][i+1] = true;
}
dp[0][0] = true;
int k;
for(int i=1; i<=m; ++i)
for(int j=1; j<=n; ++j){
k = i+j;
if(s1[i-1] == s3[k-1])
dp[i][j] = dp[i][j] || dp[i-1][j];
if(s2[j-1] == s3[k-1])
dp[i][j] = dp[i][j] || dp[i][j-1];
}
return dp[m][n];
}
};
假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30057423
)

[LeetCode] Interleaving String [30]的更多相关文章

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

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

  2. Leetcode:Interleaving String 解题报告

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

  3. [LeetCode] Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

  4. [Leetcode] Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  5. [LeetCode] Interleaving String 解题思路

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  6. [leetcode]Interleaving String @ Python

    原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...

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

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

  8. 【leetcode】Interleaving String

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

  9. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

随机推荐

  1. Maven中pom.xml常用元素说明

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  2. Max Sum of Max-K-sub-sequence(单调队列)

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. flexible.js字体大小诡异现象解析及解决方案

    最近在做一个手机端页面时,遇到了一个奇怪的问题:字体的显示大小,与在CSS中指定的大小不一致.大家可以查看这个Demo(记得打开Chrome DevTools). 就如上图所示,你可以发现,原本指定的 ...

  4. html_day1

    第一天学习,了解到html的结构和语法. html的语法:   1.所有的html标签都要放在<>尖括号里. 2.标签不分大小写 建议小写 3.标签中的属性与标签名之间要有一个空格,如多个 ...

  5. C#/.Net Post获取数据流的一种简单写法

    最近在弄一些第三方的平台,经常调用第三方的接口实现某些特定的功能 在实现的同时基本上都需要本地的数据经过服务器在Request到第三方的服务器中处理,再返回相应的数据结构体:json/xml 以下是我 ...

  6. Android Service(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  7. sqlserver2012一直显示正在还原(Restoring)和从单用户转换成多用户模式(单用户连接中)

    如果不需要还原,则使用: restore database test with recovery如果只需要还原,则使用: restore database test with norecovery U ...

  8. ios 网络数据下载和JSON解析

    ios 网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍ios中如何利用NSURLConnection从网络上下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载 ...

  9. C++程序设计实践指导1.12数组中数据线性变换改写要求实现

    改写要求1:分别用指针pa.pb代替数组 改写要求2:从键盘输入data元素 元素个数任意,输入0结束 #include <cstdlib> #include <iostream&g ...

  10. Linq的一些基础查询

    其中包括对数据中常用的条件查询,投影,分区,排序,分组,集合,元素,量词,和集集等标准查询操作符进行分类介绍 一.条件操作符 条件操作符where类似于SQL中的where子句,用于实现条件查询.下列 ...