LeetCode解题报告—— Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
思路
可以遍历s1和s2所能构成的所有可能的interlaving string。
We start by taking the current character of the first string s1 and then appending all possible interleavings of the remaining portion of the first string s1 and the second string s2
and comparing each result formed with the required interleaved string s3. Similarly, we choose one character from the second string s2 and form all the interleavings with the
remaining portion of s2 and s1 to check if the required string s1 can be formed.
意思就是说先固定s1中的第一个字符,计算s1中剩下的部分和s2组成的所有可能的interleavings ,将s1添加到每个interleavings的前面,再和s3比较。相似的,固定s2中第第一个字符,然后计算s2剩下部分和s1所能构成的所有可能的interleavings。这里就用到了递归的方法。

代码
public class Solution {
public boolean is_Interleave(String s1,int i,String s2,int j,String res,String s3)
{
if(res.equals(s3) && i==s1.length() && j==s2.length())
return true;
boolean ans=false;
if(i<s1.length())
ans|=is_Interleave(s1,i+1,s2,j,res+s1.charAt(i),s3);
if(j<s2.length())
ans|=is_Interleave(s1,i,s2,j+1,res+s2.charAt(j),s3);
return ans;
}
public boolean isInterleave(String s1, String s2, String s3) {
return is_Interleave(s1,0,s2,0,"",s3);
}
}
当然这样可能会超时。现在介绍DP,DFS,BFS三种解法。
Using 2D Dynamic Programming
仔细观察 s1,s2。比如aab,bcc。我们去构造interleaving时无非就是先从一个字符串开始,然后选去另一个字符串中取或者在当前字符串之后取。比如说,一开始interleaving是空的,那么现在往里面加字符,第一个字符可能是a,也可能是b。如果加入了a,那么下个字符可能是a或者是c。就这样按一定路径去往interleaving里面加字符直到s1,s2全部取玩即构成了一个完整的的interleaving,和s3比较即可。
将s1, s2看成是一个二维平面地图
示例:s1="aa",s2="ab",s3="aaba"。标1的为可行。最终返回右下角。
0 a b
0 1 1 0
a 1 1 1
a 1 0 1
那么取s1中的字符就变成了向下走,取s2中的字符就变成了向右走。过程中要始终保持目前的interleaving是s3的prefix才行。
dp[i][j] -- implies if it is possible to obtain a substring of length (i+j+2) which is a prefix of s3 by some interleaving of prefixes of strings s1 and s2 having lengths (i+1) and (j+1) respectively.
递推公式:
如果索引位置(i, j)上的字符不匹配 s3 中的第k个字符,注意此时有 k=i+j+1。那么不用说了,直接置为false;
如果匹配并且是s1在i位置上的字符匹配的,那么要保证s1在i之前的部分和s2在j之前(包括j)的部分能构成s3在k之前的部分,否则如果是s2中索引j处的字符匹配的,那么要保证s1在i(包括i)之前的部分和s2在j之前的部分能构成s3在k之前的部分。
代码
public class Solution {
public 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];
for (int i = 0; i <= s1.length(); i++) {
for (int j = 0; j <= s2.length(); j++) {
if (i == 0 && j == 0) {
dp[i][j] = true;
} else if (i == 0) {
dp[i][j] = dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1);
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1);
} else {
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()];
}
}
关于BFS和DFS的解释:https://leetcode.com/problems/interleaving-string/discuss/31904/Summary-of-solutions-BFS-DFS-DP
LeetCode解题报告—— Interleaving String的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- 【一天一道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解题报告5道题(七)
题目一:Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- leetcode解题报告(25):Reverse Words in a String III
描述 Given a string, you need to reverse the order of characters in each word within a sentence while ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
随机推荐
- Spark获取某个手机号在某个基站下停留的时间和当前手机所在的位置的案例
1.业务需求 在拥有手机号在每个基站处停留时间日志 和 基站信息的 算出某个手机号的(所在基站,停留时间),(当前所在经度,当前所在纬度) 其中手机连接基站产生的日志信息类似如下: 186888888 ...
- OpenCV学习笔记(01)我的第一个OpenCV程序(环境配置)
昨天刚刚考完编译原理,私心想着可以做一些与考试无关的东西了.一直想做和图像处理相关的东西,趁这段时间有空学习一下OpenCV,搭建环境真是一件麻烦的事情,搞了近三个小时终于OK了.先来张图: 大致描述 ...
- stout代码分支之十二:巧妙的EXIT
在c++中,为了便于定位问题,进程异常退出时,需要获取返回码和错误信息.stout中将这种功能巧妙的封装成EXIT类. #define EXIT(status) __Exit(status).stre ...
- python读书笔记-django架站过程总结(from the django book)
django架站过程总结:1.django-admin startproject store2.store这个project的目录下有:__init__,manage,setting,urls3.se ...
- git merge与git rebase
文章源:https://blog.csdn.net/wh_19910525/article/details/7554489 git merge是用来合并两个分支的. git merge b # 将b分 ...
- arm-linux-gcc等交叉编译工具的安装
1.软件安装 步骤1:打开虚拟机,在/usr/local/下创建/usr/local/arm文件夹(一般用户自定义程序放到这里) 步骤2:先将安装包从Windows中弄到linux中去.可以用共享文件 ...
- HTML或者JSP页面--执行完某事件后刷新页面,重置表单,清空数据
在提交表单或者执行某个事件之后,如果需要重置表单(即清空表单里的数据) 可以执行下面代码来完成 方式一: self.location.href="userController.do?goAd ...
- Linux系统查看系统信息
1. CPU # lscpu # cat /proc/cpuinfo //可以知道每个cpu信息,如每个CPU的型号,主频等 2. 内存 # free -m # cat /proc/meminfo / ...
- Independence.
It's not giving up, it's letting go, and moving to a better place. I will survive and be the one who ...
- 解决方案:WindowsError: [Error 2]
使用Python的rename()函数重命名文件时出现问题,提示 WindowsError: [Error 2] 错误,最初代码如下: def renameFile(filename): filePr ...