Interleaving String

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.

 
 
采用动态规划,假设dp[i][j]表示了s1的前i个元素(包括第i个元素)s1[1],s1[2].....s1[i-1],与s2的前j个元素(包括第j个元素)s2[1],s2[2]...s2[j-1]是否可以构成s3
 
有两种情况可以考虑,
如果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],也可以认为dp[i][j]成立
 
 
所以可以得到如下的递推式:
 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 n1=s1.length();
int n2=s2.length();
int n3=s3.length(); if(n1+n2!=n3)
{
return false;
} vector<vector<bool> > dp(n1+,vector<bool>(n2+,false)); dp[][]=true; for(int i=;i<=n1;i++)
{
dp[i][]=dp[i-][]&&s1[i-]==s3[i-];
} for(int j=;j<=n2;j++)
{
dp[][j]=dp[][j-]&&s2[j-]==s3[j-];
} for(int i=;i<=n1;i++)
{
for(int j=;j<=n2;j++)
{
dp[i][j]=(dp[i-][j]&&s1[i-]==s3[i+j-])||(dp[i][j-]&&s2[j-]==s3[i+j-]);
}
} return dp[n1][n2];
}
};

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

  1. 【leetcode】 Interleaving String (hard)

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

  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. 搭建OpenWrt开发环境(包括编译过程)

    OpenWrt是一个高度模块化.高度自动化的嵌入式linux发行版,其编译和安装过程比普通的linux发行版而言,要简单太多了.如果您是新手,您那恐惧的心大可放到肚子里,呵呵.对于新手来说最麻烦的恐怕 ...

  2. zepto源代码解读

    /** * Created by nono on 14-11-16. */ /* Zepto v1.1.4 - zepto event ajax form ie - zeptojs.com/licen ...

  3. Mysql-函数coalesce-查询为空设置默认值

    coalesce(字段,默认值) select coalesce(title,'liu') from a

  4. Maven-改变本地存储仓库位置

    修改 maven 仓库存放位置: 找到 maven 下的 conf 下的 settings.xml 配置文件,假设maven安装在D:\Server目录中.那么配置文件应该在 D:\Server\ma ...

  5. Java基础-继承-子类与父类执行顺序

    代码 public class Test { public static void main(String[] args) { new Circle(); } } class Draw { publi ...

  6. 10 个免费的 jQuery 可视化编辑器插件

    富文本编辑器,也就是所见即所得的 HTML 编辑器,是网站一个非常重要的组件,特别是对于一些内容发布网站来说.本文介绍 10 个基于 jQuery 的可视化文本编辑器. MarkitUp markIt ...

  7. Java编程思想学习(七) 抽象类和接口

    1.抽象类和抽象方法 抽象方法:不完整的,仅有声明而没有方法体. abstract void f(); 抽象类:包含抽象方法的类.(若一个类包含一个或多个抽象方法,则该类必须限定为抽象的.) 1.用抽 ...

  8. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  9. Mybatis的ResultMap的使用

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  10. MySQL 存储过程传参之in, out, inout 参数用法

    存储过程传参:存储过程的括号里,可以声明参数. 语法是 create procedure p([in/out/inout] 参数名  参数类型 ..) in :给参数传入值,定义的参数就得到了值 ou ...