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 ...
随机推荐
- UVALive 6811 Irrigation Line(二分图最小点覆盖--匈牙利算法)
题意:求最少的线可以覆盖一个由0.1两种数字组成的图中所有的1. eg: 只需要两条线即可. 分析: 1.先为上述例子的行列标号 2.若图中数字为1,则代表该数字所在的行与列有关联. 例如第r1行第c ...
- C++ 的隱式型別轉換
先上一段代碼, 這段代碼竟然可以編譯過,我的老天! class Boo { Boo(int c){ cout << "I'm Boo"; } }; void do_so ...
- Linux dd 命令
语法:dd [选项] if =输入文件(或设备名称). of =输出文件(或设备名称). ibs = bytes 一次读取bytes字节,即读入缓冲区的字节数. skip = blocks 跳过读入缓 ...
- .NET架构师技能体系
.NET架构师应该掌握什么样的技术?其实这个问题很简单,去看看招聘.NET架构师的公司的职位要求就知道了.比如:http://www.cnblogs.com/guwei4037/p/5615471.h ...
- gson小练习之嵌套复杂数据解析
package com.zf.demo; import java.util.List; import com.google.gson.Gson; public class JGson { /** * ...
- php连接ftp
PHP连接ftp,发现一个很好用的类库phpseclib.英文原文 Connecting to SFTP with PHP If you need to connect to SFTP using P ...
- jQuery实现页内查找相关内容
当需要在页面中查找某个关键字时,一是可以通过浏览器的查找功能实现,二是可以通过前端脚本准确查找定位,本文介绍通过jQuery实现的页面内容查找定位的功能,并可扩展显示查找后的相关信息. 本文以查找车站 ...
- sql新感悟(where 1 = 1)
花了好久把YII框架看完发现一本很不错的书:SQL案例解析(清华大学出版社),看到一些比较有用的东西,感觉应该把他记录下来,看了好多页发现书中一直有 where 1=1,这样的语句,查过发现“wher ...
- The content of element type "sqlMapConfig" is incomplete,
The content of element type "sqlMapConfig" is incomplete, it must match "(properties? ...
- 【BZOJ 1295】 [SCOI2009]最长距离
Description windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离. 如果从格 ...