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.

题目大意:给定三个字符串,s1,s2,s3,问s3是否由s1和s2交叉构成。

解题思路:递归超时,重复计算太多,加上cache可能可以。

说一下dp的方法,研究了半天才看明白,用一个二维布尔数组dp[i][j]表示从s1中取前i位,从s2中取前j位构成s3的前i+j位是否可以。

那么dp[i][j]可能从dp[i-1][j]或dp[i][j-1]而来:

  如果从dp[i-1][j]来,那么dp[i-1][j]应该为true,并且s1.charAt(i-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;

  如果从dp[i][j-1]来,那么dp[i][j-1]应该为true,并且s2.charAt(j-1)=s3.charAt(i+j-1)成立时,dp[i][j]=true;

初始化,dp[0][0]=true表示两个空字符串构成一个空字符串为true。

另外,从s1或s2开始构成s3的前n或m个字符也是初始条件。

举个列子,注意左上角那一对箭头指向的格子dp[1][1], 表示s1取第1位a, s2取第1位d,是否能组成s3的前两位aa

从dp[0][1] 往下的箭头表示,s1目前取了0位,s2目前取了1位,我们添加s1的第1位,看看它是不是等于s3的第2位,( i + j 位)

从dp[1][0] 往右的箭头表示,s1目前取了1位,s2目前取了0位,我们添加s2的第1位,看看它是不是等于s3的第2位,( i + j 位)

  public static 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];
dp[0][0] = true;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == s3.charAt(i)) {
dp[i + 1][0] = dp[i][0];
}
}
for (int i = 0; i < s2.length(); i++) {
if (s2.charAt(i) == s3.charAt(i)) {
dp[0][i + 1] = dp[0][i];
}
}
for (int i = 1; i < s1.length() + 1; i++) {
for (int j = 1; j < s2.length() + 1; j++) {
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()];
}

参考:http://blog.csdn.net/u011095253/article/details/9248073

Interleaving String——Leetcode的更多相关文章

  1. Interleaving String leetcode

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

  2. Interleaving String leetcode java

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

  3. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

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

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

  5. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  6. 【leetcode】Interleaving String

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

  7. LeetCode之“动态规划”:Interleaving String

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

  8. 【LeetCode】97. Interleaving String

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

  9. 40. Interleaving String

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

随机推荐

  1. ZeroMemory和memset的区别

    摘自百度百科,保存为学习使用 ZeroMemory,是美国微软公司的软件开发包SDK中的一个宏. 其作用是用0来填充一块内存区域. 声明 void ZeroMemory( PVOID Destinat ...

  2. CSS 响应式设计

    响应式设计是指在不同分辨率的设备中,网页布局可以自适应的调整.这种弹性化的布局使网站在不同设备中的布局都比较合理,可以为不同终端的用户提供更加舒适的界面和更好的用户体验,其根本理念是使原本 PC 上的 ...

  3. php返回json数据函数实例

    本文实例讲述了php返回json数据函数的用法,分享给大家供大家参考.具体方法如下: json_encode()函数用法: ? 1 echo json_encode(array('a'=>'bb ...

  4. Ogre实现简单地形

    利用SimpleRenderable实现DirectX 9 3D 游戏设计入门中 第十三章 地形渲染基础的简单地形,只是简单的实现了地形的基本框架,顶点,索引,纹理等,为简单起见高度都为1,适合新手做 ...

  5. htm、html、shtml区别

    htm.html.shtml都是静态网页的后缀,三者也可以说都是只是扩展名不同,其他一样,都是静态的网页. htm和html是完全静态的网页不通过服务器编译解释直接送出给浏览器读取的静态网页,以htm ...

  6. Js处理json数据

    js中处理由ajax调用返回的json数据问题,可以通过使用JSON.parse方法将json字符串转化成javascript 对象.通过对象访问属性值. JSON.parse 只限于高版本的浏览器. ...

  7. Tarjan系列算法总结(hdu 1827,4612,4587,4005)

    tarjan一直是我看了头大的问题,省选之前还是得好好系统的学习一下.我按照不同的算法在hdu上选题练习了一下,至少还是有了初步的认识.tarjan嘛,就是维护一个dfsnum[]和一个low[],在 ...

  8. 将excel数据导入内表的函数

    call function 'TEXT_CONVERT_XLS_TO_SAP'    exporting      i_tab_raw_data       = lt_raw "开始行    ...

  9. 移动应用产品开发-android开发(二)

    这段时间实在太忙了,也没什么精力来写博客,还是没养成写博客的习惯,还是得记载下来,不然时间久了就忘记了. 早上一大早就来公司了,趁还早,拟定今天的工作内容和计划后,赶紧记载点东西. 最近做androi ...

  10. ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据

    一.简介 运行结果 二.代码1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"? ...