https://leetcode.com/problems/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.

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

leetcode@ [97] Interleaving Strings的更多相关文章

  1. [LeetCode] 97. Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...

  2. leetcode 97 Interleaving String ----- java

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

  3. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  4. [leetcode]97. Interleaving String能否构成交错字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...

  5. Leetcode#97 Interleaving String

    原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...

  6. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  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] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  9. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

随机推荐

  1. SQLite数据库简介(转)

    大家好,今天来介绍一下SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准 ...

  2. php的redis 操作类,适用于单台或多台、多组redis服务器操作

    redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...

  3. [itint5]根据前序后序遍历统计二叉树

    http://www.itint5.com/oj/#28 这题有意思.一开始还想不清楚,看了解释,很棒. 这个题目的特殊之处是所有节点的值都是不一样的. 所以递归过程可以大大简化. 先看两种遍历的性质 ...

  4. [topcoder]ZigZag

    http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 动态规划题.如果不用DP,暴力的应当在2^ ...

  5. thinkphp 减少文件目录

    配置 'TMPL_FILE_DEPR'=>'_' 于是模板文件的格式为如:index_index.html,index_show.html .代替原来的目录结构:/index/index.htm ...

  6. asp.net 母版页使用详解--转

    http://www.cnblogs.com/_zjl/archive/2011/06/12/2078992.html 母版页是VS2005中新引入的一个概念,它很好地实现界面设计的模块化,并且实现实 ...

  7. HeadFirst设计模式之模板方法模式

    一. 1.The Template Method defines the steps of an algorithm and allows subclasses to provide the impl ...

  8. 转:tar 常用命令

    tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...

  9. SVN中update to revision与revert to revision的区别

    假设我们有许多个版本,版本号分别是1-10 如果我们在7这里选择revert to this version那么7之后的8,9,10的操作都会被消除 如果在7选择revert changes from ...

  10. Java API ——包装类

    1.包装类的概述 · 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据. · 常用的操作之一:用于基本数据类型与字符串之间的转换. · 基本类型和包装类的对应 为了对基本数据 ...