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. 6、httpd服务的安装、配置

    .本地yum源安装httpd服务 (必须是已搭建好本地yum源) yum install httpd -y (安装httpd) 2.systemctl restart httpd.service   ...

  2. js导入导出excel

    导入: <html xmlns="http://www.w3.org/1999/xhtml" > <head>      <title>Unti ...

  3. svg DOM的一些js操作

    这是第一个实例,其中讲了如何新建svg,添加元素,保存svg document,查看svg. 下面将附上常用一些元素的添加方法:(为js的,但基本上跟java中操作一样,就是类名有点细微差别) Cir ...

  4. SAP abap 需找出口(BADI)的几种方法

    需找BADI方法有很多,据公司的牛人说,他知道的就不止5种 现在给出一些比较简单的方法 首先,大家要知道,一个程序的出口不会太多,需找出口,很多的时候都是在尝试 第二,方法:首先会给出事务码,然后通过 ...

  5. mac 下配置protobuf 3.0 golang环境

    protobuf 3.0  与 之前的 protobuf 2.6 的语法是不一样的.需要重新安装一下,本机的环境是 OS X Yosemite  10.10.2 1. 不采用home brew安装,用 ...

  6. 从erase()谈起

    面试中,因为我说自己熟悉C++,就问我一个问题,Vector<int>里, 想把元素为2的节点删除掉.该怎么做. 我已经很久没有用Vector了,但是只有硬着头皮想一下,第一个想起的是re ...

  7. REDIS key notification

    Commands Clients Documentation Community Download Support License Join us in London October 19th for ...

  8. Python 基礎 - 數據類型

    標準數據類型 Python3 中有六個標準的數據類型 1 Number(數字) 2 String(字符串) 3 List (列表) 4 Tuple (元組) 5 Sets (集合) 6 Diction ...

  9. remove adapter

    Although adapter and other technical sequences can potentially occur in any location within reads, b ...

  10. python ML 笔记:Kmeans

    kmeans算法的python实现: 参考与样本来源<Machine Learning in Action> #-*-coding:UTF-8-*- ''' Created on 2015 ...