Given s1s2s3, 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.

思路:做过几道动态规划了,终于也有了点感觉。一次就AC了,好开心啊~

用dp[m][n]来存储s3[0 ~ m+n-1]是否是s1[0~m-1]与s2[0~n-1]交替组成的。注意,没有必要存s3的维度,因为s1[0~m-1]与s2[0~n-1]只能匹配s3的m+n个字符,即第三个维度是确定的。

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]

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length(); if(len3 != len1 + len2)
return false;
vector<vector<bool>> dp(len1 + , vector<bool>(len2 + , false));
dp[][] = true;
for(int i = ; i < len1 + ; i++)
{
dp[i][] = dp[i-][] && (s1[i-] == s3[i-]);
}
for(int j = ; j < len2 + ; j++)
{
dp[][j] = dp[][j-] && (s2[j-] == s3[j-]);
}
for(int i = ; i < len1 + ; i++)
{
for(int j = ; j < len2 + ; j++)
{
dp[i][j] = (dp[i-][j] && (s1[i-] == s3[i+j-]))
|| (dp[i][j-] && (s2[j-] == s3[i+j-]));
}
}
return dp[len1][len2];
}
};

【leetcode】 Interleaving String (hard)的更多相关文章

  1. 【leetcode】Interleaving String

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

  2. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  3. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】984. String Without AAA or BBB 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...

  5. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  6. 【leetcode】 Scramble String (hard)★

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  8. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  9. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

随机推荐

  1. .NET逻辑分层架构总结

    一.基础知识准备: 1.层的原则: (1)每一层以接口方式供上层调用. (2)上层只能调用下层. (3)依赖分为松散交互和严格交互两种. 2.业务逻辑分类: (1)应用逻辑. (2)领域逻辑. 3.采 ...

  2. Jquery实现滚动显示欢迎字幕效果

    Jquery控制滚动显示欢迎字幕: 参考代码: <!DOCTYPE html> <html> <head> <title>Colin Marquee W ...

  3. Excel 使用宏批量修改单元格内指定文字为红字

    -> step 1:新建宏,进入编辑,使用如下代码: Sub Ss()Dim c As RangeFor Each c In ActiveSheet.UsedRange i = 1 While ...

  4. hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))

    Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. Mahout 介绍

    1.Hbase+k-means  (G级别) 2.k-means+mr (T级别) 1. 2.canopy 2.贝叶斯算法 决策,分类,文档分类 3.推荐系统 4.图书推荐系统 1.需求 付完款的用户 ...

  6. Ubuntu 12 编译安装 PHP 5.4 及 问题汇总

    参考先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 安装过程: ############################ ...

  7. iOS开发——多线程篇——RunLoop

    一.简介 1.什么是RunLoop从字面意思看运行循环跑圈 基本作用保持程序的持续运行处理App中的各种事件(比如触摸事件.定时器事件.Selector事件)节省CPU资源,提高程序性能:该做事时做事 ...

  8. iOS开发——UI基础-按钮内边距,图片拉伸

    一.内边距 UIButton有三个属性,分别可以设置按钮以及内部子控件的内边距 1.contentEdgeInsets 如果是设置contentEdgeInsets, 会把UIImageView和UI ...

  9. 五款最佳Linux下载管理器推荐

    导读 新的Linux用户从Windows转换过来时面临的困难之一就是,找到一款优秀的下载管理器.如果你是或曾经是Windows用户,可能熟悉互联网下载管理器(IDM).下载加速器Plus(DAP)之类 ...

  10. Android学习笔记(十七)——数据库操作(下)

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 这一次我们来试一试升级数据库,并进行数据库的CRUD操作,其中, C 代表添加(Create) ,R 代表查询 ...