题目:

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. javascript-对象的创建(一)

    <!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF ...

  2. mysql的时间函数

    from_unixtime()是MySQL里的时间函数 date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d' 主要是将返回值 ...

  3. 进度条轮播【BackgroundColor】

    直接贴代码先看 HTML: <div class="bannar"> <div class="img"> <ul> < ...

  4. VS2010 常见错误总结

    错误一:“此时无足够的可用内存,无法满足操作的预期要求,可能是由于虚拟地址空间碎片造成的,请稍后重试” 安装VS2010补丁:http://xiazai.jb51.net/201007/tools/V ...

  5. c#的协变和逆变

    关于协变和逆变要从面向对象继承说起.继承关系是指子类和父类之间的关系:子类从父类继承,所以子类的实例也就是父类的实例.比如说Animal是父类,Dog是从Animal继承的子类:如果一个对象的类型是D ...

  6. JavaScript的语法要点 3 - Calling Context

    上一篇讲了JavaScript的Scope Chain - 每一个函数都有一个scope chain与之关联,scope chain上有第一个对象维护着本地变量作为其属性.另外我们在JavaScrip ...

  7. 为 Web 设计师准备的 25+ 款扁平 UI 工具包

    Flat UI Kit by Riki Tanone (free) Flat UI Kit (PSD) by Devin Schulz (free) Eerste UI Kit (free) Metr ...

  8. 1064. Complete Binary Search Tree

    二叉排序树: http://www.patest.cn/contests/pat-a-practise/1064 #include <iostream> #include <vect ...

  9. [译] ASP.NET 生命周期 – ASP.NET 上下文对象(五)

    ASP.NET 上下文对象 ASP.NET 提供了一系列对象用来给当前请求,将要返回到客户端的响应,以及 Web 应用本身提供上下文信息.间接的,这些上下文对象也可以用来回去核心 ASP.NET 框架 ...

  10. Get the item a SharePoint workflow task is associated with

    This is handy. SharePoint helpfully populates the meta data with the GUID of the list and the ID of  ...