题目:

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.

链接:  http://leetcode.com/problems/interleaving-string/

题解:

依然是使用dynamic programming。和Edit Distance很像。假设我们构建一个棋盘,s1代表行,s2代表列,每次只能向右或者向下走,最后看s3这条路径能不能够从左上到达右下。

Time Complexity - O(m * n), Space Complexity - O(m * n)。

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

还看到有大神用BFS来做,原理其实和DP差不多,复杂度也基本一样,列在参考里了。

题外话:  从9月12号开始,到今天为止,基本leetcode的第一遍就做了100题了,有两道没做,Text Justification和Wild Card Matching, 打算学一学Automata以后再来挑战这几道。同时还想学习一下Mining Massive Datasets以及基本的Python编程。 目标很多,时间很少,希望一切顺利吧。(10-12-2015)。

Reference:

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

https://leetcode.com/discuss/19973/8ms-c-solution-using-bfs-with-explanation                  <- BFS

https://leetcode.com/discuss/22726/dp-solution-in-java

https://leetcode.com/discuss/4667/is-there-a-better-algorithm-than-what-i-give

https://leetcode.com/discuss/11694/my-dp-solution-in-c

https://leetcode.com/discuss/16086/my-solution-in-java-using-dp-time-o-n-m-and-space-o-m

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 ----- java

    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 s1 and s2. Input: s1 = "aabc ...

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

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

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

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

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

  9. Leetcode#97 Interleaving String

    原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...

随机推荐

  1. 查找-find -grep

    find#.#-name#"*pc"#|#xargs#grep#"Flag" “*.pc”设置要找的文件名grep后面是要找的字符串 #是空格

  2. Sublime Text 破解

    引言 放假三天,呆家里把win7换成了win8.1,接着玩起了hyperv,试着装了个windows xp虚拟机,体验很不错.不过对linux系统的支持不怎么样,装了个ubuntu,体验相当差!闲着无 ...

  3. 【转】VS2012发布网站详细步骤

    1.打开你的VS2012网站项目,右键点击项目>菜单中 重新生成一下网站项目:再次点击右键>发布: 2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 输入你自 ...

  4. 0-1背包问题与N皇后问题的纠结

    昨日同学要我帮他看一道算法,如下: 是不是乍一看是“0-1背包”问题呀,我也这么想,于是就这么兴致勃勃的开始用这个想法去思考怎么算.但是算法也忘得差不多,回去赶紧补补,也趁着这次机会好好复习一下算法, ...

  5. JavaScript jQuery 事件、动画、扩展

    事件 因为JavaScript在浏览器中以单线程模式运行,页面加载后,一旦页面上所有的JavaScript代码被执行完后,就只能依赖触发事件来执行JavaScript代码. 浏览器在接收到用户的鼠标或 ...

  6. [DevExpress]ChartControl之饼状图百分比示例

    关键代码: using System; using System.Data; using System.Windows.Forms; using DevExpress.XtraCharts; name ...

  7. github项目filter_firewall说明

    本文编写的目的: 本文是对上传到github上的项目进行说明.github链接:filter_firewall有任何意见或者建议可以Email:18277973721@sina.cn 项目介绍: 包过 ...

  8. web.xml常用元素

    web.xml文件是用来初始化配置信息:比如welcome页面.servlet.servlet-mapping.filter.listener.启动加载级别等.当你的web工程没用到这些时,你可以不用 ...

  9. csu 1306 Manor(优先队列)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1306 1306: Manor Time Limit: 1 Sec  Memory Limit: 1 ...

  10. 6779. Can you answer these queries VII - SPOJ

    Given a tree with N ( N<=100000 ) nodes. Each node has a interger value x_i ( |x_i|<=10000 ). ...