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. golang的第一个deadlock

    package main import (     "fmt"     "math/rand" ) func push(c chan []int) {      ...

  2. mysql 中execute、executeQuery和executeUpdate之间的区别

    在用纯JSP做一个页面报警功能的时候习惯性的用executeQuery来执行SQL语句,结果执行update时就遇到问题,语句能执行,但返回结果出现问题,另外还忽略了executeUpdate的返回值 ...

  3. UNIX 和 LINUX

    UNIX操作系统(尤尼斯),是一个强大的多用户.多任务操作系统,支持多种处理器架构,按照操作系统的分类,属于分时操作系统,最早由KenThompson.DennisRitchie和DouglasMcI ...

  4. GridView控件中加自动排列序号

    GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField  HeaderText="序号">    < ...

  5. JSP应用程序(自定义错误页面)

    一.编写 1.index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> &l ...

  6. C++ UFunction({FLAG}) 宏 FLAG 解释笔记

    1.BluePrintCallable  --蓝图可调用 但不可编辑 2.BlueprintImplementableEvent --函数体必须实现与Blueprint 但函数名必须生成与C++ .h ...

  7. 网络编程-socket

    本节内容: 一:TCP/IP:Transmission Control Protocol/Internet Protocol 传输控制协议/因特网互联协议.即通讯协议.是主机接入互联网以及互联网中两台 ...

  8. 在maven项目中解决第三方jar包依赖的问题

    在maven项目中,对于那些在maven仓库中不存在的第三方jar,依赖解决通常有如下解决方法: 方法1:直接将jar包拷贝到项目指定目录下,然后在pom文件中指定依赖类型为system,如: < ...

  9. python数据结构与算法——链表

    具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...

  10. C++ Primer : 第十三章 : 拷贝控制之拷贝控制和资源管理

    定义行为像值的类 行为像值的类,例如标准库容器和std::string这样的类一样,类似这样的类我们可以简单的实现一个这样的类HasPtr. 在实现之前,我们需要: 定义一个拷贝构造函数,完成stri ...