Given s1, s2, s3, 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) {
//dp[i][j]: s3[i+j-1] can be interleaved by s1[0..i], s2[0..j]
//dp[i][j] = dp[i-1][j] if s3[i+j-1]==s1[i]
// = dp[i][j-1] if s3[i+j-1]==s2[j]
//so traverse i,j,k from small to large
int len1 = s1.length();
int len2 = s2.length();
if(len1+len2!=s3.length()) return false;
vector<vector<bool>> dp(len1+, vector<bool>(len2+, false)); //initial status
dp[][] = true; //0 means null string
for(int j = ; j <= len2; j++){
dp[][j] = dp[][j-] && s2[j-]==s3[j-];
}
for(int i = ; i <= len1; i++){
dp[i][] = dp[i-][] && s1[i-]==s3[i-];
} //state transfer
for(int i = ; i<= len1; i++){
for(int j = ; j <= len2; j++){
dp[i][j] = (dp[i][j-] && s2[j-]==s3[i+j-]) || (dp[i-][j] && s1[i-]==s3[i+j-]);
}
}
return dp[len1][len2];
}
};

97. Interleaving String (String; DP)的更多相关文章

  1. leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]

    1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...

  2. hdu6194 string string string

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6194 题目: string string string Time Limit: 2000/10 ...

  3. HDU 6194 string string string(后缀数组+RMQ)

    string string string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. 入门:Java Map<String,String>遍历及修改

    重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...

  5. 关于 Dictionary<string,string>,和List<T>在View的使用

    在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...

  6. alibaba fastjson List<Map<String, String>>2Str

    import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...

  7. getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:

    直接遍历报错:[Ljava.lang.String;@44739f3f Map<String, String> tempMap = new HashMap<String, Strin ...

  8. The constructor User.Student(String, String, String) is not visible

    项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ...

  9. 从为什么String=String谈到StringBuilder和StringBuffer

    前言 有这么一段代码: public class TestMain { public static void main(String[] args) { String str0 = "123 ...

随机推荐

  1. poj 3253 Fence Repair (贪心,优先队列)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  2. Mac部分软件安装教程

    1.安装Office Office破解版安装教程:https://www.jianshu.com/p/f45894b67ec7 2.安装破解版ps 1.安装ps,最后开始试用 2.解压缩Adobe z ...

  3. [UE4]把枪抽象为一个类

  4. [UE4]C++调用蓝图函数:BlueprintImplementableEvent函数说明符用法

    用BlueprintImplementableEvent标明的函数在C++代码中不需要有方法体,方法体在蓝图中实现. 用法: 1,现在C++头文件中定义函数名 UFUNCTION(BlueprintI ...

  5. Hibernate inverse反转

    inverse: inverse: 指定由哪一方来维护之间的关联关系 false默认,表示不放弃,是主动放 true:表示把关联关系的维护反转(放弃),对集合对象的修改不会被反映到数据库中 容易出现的 ...

  6. Java 集合 线程安全

    Java中常用的集合框架中的实现类HashSet.TreeSet.ArrayList.ArrayDeque.LinkedList.HashMap.TreeMap都是线程不安全的,如果多个线程同时访问它 ...

  7. 小项目,吃货联盟,java初级小项目,源代码

    1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...

  8. springboot整合ribbitMQ

    参考:https://blog.csdn.net/a13627210064/article/details/82348059 参考:https://blog.csdn.net/u010288264/a ...

  9. objective C, parse json时注意事项

    例: dict为从API请求返回的json调用 NSJSONSerialization JSONObjectWithData:方法得到的NSDictionary实例. 当执行以下语句时linkStri ...

  10. cobbler配置解析

    1.Cobbler命令说明: 命令名称 命令用途 cobbler check 检查cobbler配置 cobbler list 列出所有的cobbler元素 cobbler report 列出元素的详 ...