题目

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. Android系统默认Home应用程序(Launcher)的启动过程源代码分析

    在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还需要有一个 Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home ...

  2. SSO之CAS基础及应用视频教程(2)

    CAS介绍 CAS = Central Authentication Service,中央认证服务.CAS 是 Yale 大学发起的一个开源项目,能够为 Web 应用系统或者非Web应用系统提供一种可 ...

  3. Eclipse error:Access restriction

    报错:Access restriction: The method decodeBuffer(String) from the type CharacterDecoder is not accessi ...

  4. im2uint8函数分析

    环境:Win7 64位 + Matlab R2010a 本次分析的函数为im2uint8,这个函数在图像处理中要用到,主要把图像数据类转换到uint8 uint8函数有效的输入的图像数据类为:logi ...

  5. git commit的--amend选项

    git commit --amend常常用来修改某个branch上最顶端的commit,大多数情况下,这个命令给人的感觉是用新的commit替换了原来的commit.git commit --amen ...

  6. 数据库操作CURD

    JDBCCURD操作实例 19. 五 / J2EE / 没有评论   代码目录结构: domain   javabean: util   工具类  jdbcUtil是连接数据mysql数据库的工具类 ...

  7. 安装mysqlsla性能分析工具

    开启mysql慢查询日志 vi /etc/my.cnf slow-query-log = on  #开启MySQL慢查询功能 slow_query_log_file = /data/mysql/127 ...

  8. php过滤iphone的emoji表情

    public static function removeEmoji($text) { $clean_text = ""; // Match Emoticons $regexEmo ...

  9. php 实现传入参数的传出

    类似于.net的out功能,php中可以使用&实现 如下示例: <?php$x=2; inOutFunction($x); function inOutFunction(&$x) ...

  10. 【5】将服务部署到本机(Ubuntu14.04)

    首先,先将文件复制到指定的文件夹 我这里选择在/var下面新建一个www的文件夹来存放 复制BLOG文件夹的内容到www文件夹下 sudo cp -r /home/jakeychen/Jakey/Bl ...