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.

思想: 动态规划。

DP[i][j] = (DP[i-1][j] && s1[i-1] == s3[i+j-1]) || (DP[i][j-1] && s2[j-1] == s3[i+j-1]); 其中,i, j 分别为字符串 s1, s2 的当前长度。

方法一:

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s1.size() + s2.size() != s3.size()) return false;
if(s1 == "") return s2 == s3;
if(s2 == "") return s1 == s3;
vector<vector<bool> > DP(s1.size()+1, vector<bool>(s2.size()+1, false));
DP[0][0] = true;
for(size_t i = 1; i <= s1.size(); ++i) {
DP[i][0] = (DP[i-1][0] && s1[i-1] == s3[i-1]);
for(size_t j = 1; j <= s2.size(); ++j) {
DP[0][j] = (DP[0][j-1] && s2[j-1] == s3[j-1]);
DP[i][j] = (DP[i-1][j] && s1[i-1] == s3[i+j-1]) || (DP[i][j-1] && s2[j-1] == s3[i+j-1]);
}
}
return DP[s1.size()][s2.size()];
}
};

方法二: 进一步优化,空间复杂度, O(min(s1.size(), s2.size()));

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
if(s1.length() + s2.length() != s3.length()) return false;
if(s1 == "") return s2 == s3;
if(s2 == "") return s1 == s3;
if(s2.size() > s1.size()) s1.swap(s2); // make the sapce O(min(s1.size(), s2.size()));
vector<bool> DP(s2.size()+1);
for(size_t i = 0; i <= s1.size(); ++i) {
DP[0] = (!i || (DP[0] && s1[i-1] == s3[i-1]));
for(size_t j = 1; j <= s2.size(); ++j) {
DP[j] = (DP[j] && s1[i-1] == s3[i+j-1]) || (DP[j-1] && s2[j-1] == s3[i+j-1]);
}
}
return DP[s2.size()];
}
};

40. Interleaving String的更多相关文章

  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 String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  3. 二维动态规划——Interleaving String

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

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

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

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

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

  6. Leetcode:Interleaving String 解题报告

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

  7. 【LeetCode】97. Interleaving String

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

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

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

  9. Interleaving String

    https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by th ...

随机推荐

  1. linux下用phpize给PHP动态添加扩展

    使用php的常见问题是编译php时忘记添加某扩展,后来想添加扩展,但是因为安装php后又装了一些东西如PEAR等,不想删除目录重装,这里就需要用到phpize了. 如我想增加bcmath扩展的支持,这 ...

  2. cocopods 知识集合 及 一个 好的 国外iOS技术翻译站

    http://www.exiatian.com/cocoapods%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8%E5%8F%8A%E9%85%8D%E7%BD%AE%E7% ...

  3. 局域网单机部署双tomcat内外网不能访问防火墙问题查出来

    tomcat部署的项目内网访问不了 win7  64 位 控制面板 - 安全 -防火墙-入站规则- 新建规则 选中“端口”按钮,点选“下一步”: 选择“TCP”按钮,在“特定本地端口”输入tomcat ...

  4. javascript的坑

    1 for in循环:使用它时,要主要遍历的是所有可枚举的属性(实例以及原型中的属性) function Person(name){ this.name = name; } Person.protot ...

  5. (转)iOS图片拉伸技巧

    ( 原文博客地址:  http://blog.csdn.net/q199109106q/article/details/8615661) 纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要 ...

  6. mysql表分区(摘自 MySQL表的四种分区类型)

    一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了. 如:某用户表的记录超过了600万条,那么就可以根据入库日期将表分区,也可以根据所在地将表分区 ...

  7. HTTP协议(缓存机制Cache)

    HTTP的缓存 至于响应消息的实体,与请求消息的实体内容相似,这里只借绍下User-Agent头 User-Agent头域的内容包含发出请求的用户信息. Cache-Control头域(请求和应答通用 ...

  8. linux之tmpwatch命令

    系统使用时间长后会产生临时文件(/tmp下),需要清理.但清理的时候不推荐使用rm -rf.这样有时会引起程序的僵死. tmpwatch的说明: [root@AY121231034820cd91077 ...

  9. 2、C#入门第2课

    1.XML文件读取 XmlDocument xml; string path = "F:\\C#\\功2016.xml"; if (System.IO.File.Exists(pa ...

  10. lua遍历文件夹, zerobrane下载

    参考的这个http://www.cnblogs.com/jiufangding/p/3931585.html,配合批处理. zerobrane下载(上一篇博客忘掉了): http://files.cn ...