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. xcode7 __weak 导致报错 is unavailable

    Build Settings Apple LLVM7.1 Laguage-Object-c Weak References in Manual Retain Release 选 Yes;

  2. BZOJ-3231 递归数列 矩阵连乘+快速幂

    题不是很难,但是啊,人很傻啊...机子也很鬼畜啊... 3231: [Sdoi2008]递归数列 Time Limit: 1 Sec Memory Limit: 256 MB Submit: 569 ...

  3. 【poj1182】 食物链

    http://poj.org/problem?id=1182 (题目链接) 题意 中文题 Solution 带权并查集. 神犇博客,秒懂 fa记录父亲,r记录与父亲的关系.%3运用的很巧妙. 代码 / ...

  4. ELF(Executable and Linkable Format)

    目录 . 引言 . ELF文件格式 . ELF格式分析工具 0. 引言 0x1: ELF文件类型 ELF文件标准里把系统中采用ELF格式的文件归为以下几类 . 可重定位文件(Relocatable F ...

  5. STL Iterators

    Summary of Chapter 33 STL Iterators from The C++ Programming Language 4th. Ed., Bjarne Stroustrup. - ...

  6. UVA1025---A Spy in the Metro(DP)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 Secret agent Maria was sent to Alg ...

  7. javascript中this的学习总结

    在开发中,this多使用在function函数中,也正是由于调用function的对象的不同,才导致了this的指向不同.需要明白(1).function也是对象:(2).function执行时是在某 ...

  8. cmd批处理常用符号详解

    cmd批处理常用符号详解 作者: 字体:[增加 减小] 类型:转载 我们在批处理编写过程中经常遇到各种特殊符号,很多朋友不是很清楚cmd中特殊符号的含义,这里简单的介绍下,方便需要的朋友   1.@一 ...

  9. MySQL日期数据类型、MySQL时间类型使用总结

    MySQL:MySQL日期数据类型.MySQL时间类型使用总结 MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围 ------------ --- ...

  10. GPRS GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可用的一种移动数据业务,属于第二代移动通信中的数据传输技术

    GPRS 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可 ...