Given s1s2s3, 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的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  5. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  6. leetCode解题报告5道题(七)

    题目一:Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...

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

  8. 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 ...

  9. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

随机推荐

  1. 20165218 实验一 Java开发环境的熟悉

    实验一 Java开发环境的熟悉 课程:java程序设计 姓名:赵冰雨 学号:20165218 指导教师:娄嘉鹏 实验日期:2018.4.2 实验密级:Java开发环境的熟悉 实验内容.步骤与体会: ( ...

  2. App.config的典型应用

    ----.net中XML的典型应用 第一种修改方式: 添加xml节点figguration内容, 微软提供了一种方案来读取connectionStrings里的内容 这样就可以拿到连接sql serv ...

  3. 【树形DP】【P1364】医院放置

    传送门 Description 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接 ...

  4. 表单验证:nice Validator

    nice Validator使用文档:http://niceue.com/validator/ 一.自定义验证规则: //大类表单新增修改验证提交 $("#addbigCategory&qu ...

  5. navicat for mysql无法连接数据库和连接数据库慢的问题

    首先在自己虚拟机上登录mysql: mysql -uroot -p 然后赋予权限 GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '1234 ...

  6. kubernetes创建资源对象yaml文件例子--pod详解

    apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...

  7. http学习 - 缓存

    对缓存的理解更加深刻,缓存有一个过期时间,现在用的比较多的是 max-age,以前使用 expirt之类的, 然后就是需要向服务器验证是否是最新的,如果不是最新的则需要更新.

  8. 2015/9/10 Python基础(11):错误和异常

    程序在执行的过程中会产生异常,出现错误在以前的一个时期是致命的,后来随着程序的发展,使得一些错误的处理方式是柔和的,发生错误会产生一些错误的诊断信息和一些模糊的提示.帮助我们来处理异常.今天将学习Py ...

  9. 「6月雅礼集训 2017 Day4」暴力大神hxx

    [题目大意] 给出一个n重循环,每重循环有范围$[l, r]$,其中$l$,$r$可能是之前的变量,也可能是常数.求循环最底层被执行了多少次. 其中,保证每个循环的$l$,$r$最多有一个是之前的变量 ...

  10. Docker explainations

    What does docker run --link mean, what's the usage? link 是在两个contain之间建立一种父子关系,父container中的web,可以得到子 ...