97. Interleaving String
题目:
Given s1, s2, s3, 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的更多相关文章
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 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 = ...
- [leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
- 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 = ...
- 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 = ...
- 97. Interleaving String(字符串的交替连接 动态规划)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...
- Leetcode#97 Interleaving String
原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...
随机推荐
- InnoDB 离线转储工具
一,应用场景; 1,表空间严重损坏,无法恢复;2,数据库表空间文件丢失后从磁盘上打捞出部分数据页面;3,恢复删除记录; 二,功能; 从数据页中直接转储出文本格式的行数据,从而可以后期用 LOAD DA ...
- 运用百度开放平台接口根据ip地址获取位置
使用百度开放平台接口根据ip地址获取位置 今天无意间发现在百度开放平台接口,就把一段代码拿了下来,有需要的可以试试看:http://opendata.baidu.com/api.php?query=5 ...
- cxgrid footer summary value by a column
var AIndex: integer; AValue: variant; begin with cxGrid1DBTableView1.DataController.Summary do begin ...
- Dapper.ColumnMapper 的使用
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...
- lazy instructor
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- Jsonp 跨域请求实例
关于jsonp的一个实例,其实自己也不是很了解,今天下午稍微研究了一下: 简单来说,jsonp就是为了两个不同网站之间数据传递而产生的,主要用于js脚本,因为浏览器本身是禁止跨域访问的: 本机实例: ...
- 快捷设置IE代理小工具
时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...
- cocos2dx-Lua中出现的问题
1,在Lua中print输出失效的问题 在main.lua中添加print=release_print :
- Experience all that SharePoint 15 has to offer. Start now or Remind me later.
$spSite = Get-SpSite($waUrl); $spSite.AllowSelfServiceUpgrade = $false
- windows server 2008 r2电脑历史操作记录
1.看计算机哪天运行过. 在系统盘下的Windows\Tasks文件夹下找到文件SCHEDLGU.TXT. 2.看你最近打开过什么文件(非程序)或者文件夹 开始-->运行--> ...