题目链接:https://leetcode-cn.com/problems/interleaving-string/description/

参考链接:https://blog.csdn.net/u011095253/article/details/9248073

     https://www.cnblogs.com/springfor/p/3896159.html

首先看到字符串的题目:  “When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”如果是子字符串和字符串匹配应该想到动态规划。

dp[i][j]表示s1取前i位,s2取前j位,是否能组成s3的前i+j位。

比如:s1="aabcc"  s2="dbbca" s3="aadbbcbcac"

dp的数组如上图所示。

dp[0][0]=1;

dp[0][1]:使用s1的第一个字符'1'可以组成s3的第一个字符。然后第二也是如此;aab!=aad。后面的也是一样。

对于dp[i][i]的状态显然是由两个方向的状态来决定的。dp[i][i]是由dp[i-1][j]和dp[i][j-1]来决定的。

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

动态规划之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. 97. Interleaving String(字符串的交替连接 动态规划)

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

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

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

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

  6. 97. Interleaving String

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

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

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

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

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

随机推荐

  1. 树结构控件实例 TreeControl

    树结构控件实例 书:157 <?xml version="1.0" encoding="utf-8"?> <s:Application xml ...

  2. opencv之模糊处理

    初学OpenCV的开发者很容易被OpenCV中各种滤波方法所困扰,不知道到底该用哪里一个来做滤波.表面原因看起来是因为OpenCV中各种滤波方式实在是太多太杂, 其背后原因是对各种滤波方法的应用场景认 ...

  3. php实现记住密码下次自动登陆

    这篇博客里面还写到 实现“记住我的登录状态”的功能方法,简言之,就是对首先对session进行用户信息赋值,检测session,失效后,利用cookie对其赋值: 在实现过程中,根据网上一些代码贴,整 ...

  4. mysql----------阿里云RDS导入导出

    1.这是阿里云rds如何将导出的物理备份文件,导入到自建库里面: https://help.aliyun.com/knowledge_detail/5973700.html?spm=5176.7766 ...

  5. MySQL.ERROR 1133 (42000): Can't find any matching row in the user table

    ERROR 1133 (42000): Can't find any matching row in the user table 今天在执行  grant all privileges on cac ...

  6. 简易C# socket

    服务器 using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Thread ...

  7. hbase-java-api001

    package api; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfig ...

  8. 《大话设计模式》c++实现 装饰者模式

    一.UML图   介绍 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创 ...

  9. rpgmakermv(8) XY_TitleMenu插件

    插件作用:设置标题 /*: * @plugindesc v1.00 Display Multiple Menu in Title Screen. * @author XueYu Plugins * * ...

  10. 【转】SQL Server 运行状况监控SQL语句

    SQL Server 运行状况监控SQL语句   Microsoft SQL Server 2005 提供了一些工具来监控数据库.方法之一是动态管理视图.动态管理视图 (DMV) 和动态管理函数 (D ...