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. Ehcache 整合Spring 使用页面、对象缓存(转载)

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  2. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  3. 【leetcode】Dungeon Game (middle)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  4. POJ 1220 NUMBER BASE CONVERSION(较复杂的进制转换)

    题目链接 题意 : 给你一个a进制的数串s,让你转化成b进制的输出. A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61,0到9还是原来的 ...

  5. zoj 3640 Help Me Escape 概率DP

    记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  6. ERDAS IMAGINE 9.2安装破解方法

    Install the application. Copy the license.dat and ERDAS.exe to C:\Program Files\Leica Geosystems\Sha ...

  7. python学习笔记六--用户自定义类

    一.类: 1. 面向对象. 2. 定义了新的对象类型. 定义了两个属性:name,pay 定义了两个方法:lastName,giveRaise

  8. http_build_query函数(学习)

    http_build_query函数   http_build_query -- 生成 url-encoded 之后的请求字符串 描述 string http_build_query ( array ...

  9. centos 如何用 rsyslog 搭建本地日志服务

    一.问题背景 最近项目遇到一个问题,服务器响应很慢,team中的两个有经验的工程师找了一台服务器分析了一下,发现问题出在磁盘写入过于频繁.这里大概介绍一下背景,我们的服务器上面主要是跑各种PHP接口, ...

  10. poj 2253 Frogger(最短路 floyd)

    题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...