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.

动态规划

 public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s3.length() != s1.length() + s2.length()) {
return false;
}
boolean dp[][] = new boolean[s1.length() + 1][s2.length() + 1];
for (int i = 0; i <= s1.length(); i++) {
for (int j = 0; j <= s2.length(); j++) {
if (i == 0 && j == 0) {
dp[i][j] = true;
} else if (i == 0) {
dp[i][j] = dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1);
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1);
} else {
dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
}
return dp[s1.length()][s2.length()];
}
}

97. Interleaving String(字符串的交替连接 动态规划)的更多相关文章

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

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

  2. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

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

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and 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. 动态规划之97 Interleaving String

    题目链接:https://leetcode-cn.com/problems/interleaving-string/description/ 参考链接:https://blog.csdn.net/u0 ...

  6. 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串

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

  7. 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 = ...

  8. 97. Interleaving String

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

  9. 97. Interleaving String (String; DP)

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

随机推荐

  1. Huber-Markov先验模型相关

    随机概率重建-MAP算法 随机概率重建:利用贝叶斯理论作为框架,理想图像的先验知识作为约束条件进行图像重建.常用的随机概率超分辨率重建包括最大后验概率估计法(MAP)和极大似然估计法(ML). MAP ...

  2. 让所有IE支持HTML5的解决方案

    自从HTML5能为我们的新网页带来更高效洁净的代码而得到更多的关注,然而唯一能让IE识别那些新元素(如<article>)的途径是使用HTML5 shiv,感谢remy sharp为我们提 ...

  3. Oracle的归档日志

    归档模式的特点和要求 在归档模式下,当LGWR后台进程的写操作从一个重做日志组切换到另一个重做日志组后,归档写后台进程(ARCH/ARCRn)就会将原来的重做日志的信息复制到归档日志文件中. 可以把归 ...

  4. MD5骨骼动画模型加载

    前面我们分析了静态模型OBJ格式,桢动画模型MD2,这篇主要分析骨骼动画MD5的一些概念并且实现. 混合桢动画有计算简单,容易实现等优点,但是在需要比较细致的效果时,则需要更多的关键桢,每桢都添加相同 ...

  5. js让程序暂停运行的方法

    //自己写的暂停毫秒数的函数 function pauseTime(millTime) { var start=Date.now(); while(true){ var nowTime=Date.no ...

  6. js的等于号==的判断

    var str=0; str == "" 将返回true:

  7. Hibernate如何执行存储过程?

    Hibernate如何执行存储过程? @Overridepublic Boolean setVarValue(final String processInstanceId, final String ...

  8. 170421、maven自定义变量及属性

    一.自定义变量 <!-- 全局属性配置 --> <properties> <project.build.name>tools</project.build.n ...

  9. Java代码规范、基本类型和实例演练

    1.代码分段 当一个方法内部的代码超过7行时,就要考虑分成段落. 使用空行分隔代码 按照代码的功能进行分段 最终效果是一眼就能在宏观上把握代码的结构 (1)举例 Card 分成2段 第一段:定义变量保 ...

  10. HDU 5876 大连网络赛 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) T ...