Given three strings: s1s2s3, determine whether s3 is formed by the interleaving of s1 and s2.

Example

For s1 = "aabcc", s2 = "dbbca"

  • When s3 = "aadbbcbcac", return true.
  • When s3 = "aadbbbaccc", return false.

For this problem, if we think about the problem in the combination way then we have l1 = s1.length(); l2 = s2.length(); l3 = s3.length(); We have O(2^(l3)) ways to chose appropriate char to the certain slot then we can construct the s3. Due to the requirement of the problem we have want to use additional memory to store the repeated calculated result which could be used to solve larger problem with trivial manipulation directly. So this problem, we could use two dimentional array to store if the first i chars from s1 and first j chars from s2 could be used to construct the first i+j chars of s3. The result will be boolean true or false.

  1. state interleaving[i][j] is the result whether first i chars from s1 and first j chars form s2 could be used to construct the first i+j chars in s3.
  2. interleaving[i][j] = interleaving[i-1][j] && s1.char(i-1) == s3.char(i+j -1) || interleaving[i][j-1] && s2.char(j-1) == s3.char(i+j -1). In this way the result of interleaving[i][j] is depending on: 1) the boolean whether last char of s1 and s3 equals && the situation interleaving[i-1][j] could be achieve 2)the boolean whether last char of s2 and s3 equals && the situation interleaving[i][j-1] could be achievedBecause this problem want we to see if there is way to achieve it, as long as one of the condition satisfied, return true;
  3. Initialize the boolean array. interleaving[0][0] = true
  4. Solve interleaving[l1][l2]
public class Solution {
/**
* Determine whether s3 is formed by interleaving of s1 and s2.
* @param s1, s2, s3: As description.
* @return: true or false.
*/
public boolean isInterleave(String s1, String s2, String s3) {
// write your code here
int l1 = s1.length();
int l2 = s2.length();
//edge case when the l1+l2 does not equals to the s3 length return false
if (l1 + l2 != s3.length()) {
return false;
}
//Define the state that boolean isInterleave[i][j] stand for i-length s1
//and j-length s2 could be combined to construct a i+j-length word s3
boolean[][] isInterleave = new boolean[l1+1][l2+1];
isInterleave[0][0] = true;
//initialize all situations that use s1 to construct s3
for (int i = 1; i <= l1; i++) {
isInterleave[i][0] = (s1.charAt(i-1) == s3.charAt(i-1))
&& isInterleave[i-1][0];
}
//initialize all situations that use s2 to construct s3
for (int i = 1; i <=l2; i++) {
isInterleave[0][i] = (s2.charAt(i-1) == s3.charAt(i-1))
&& isInterleave[0][i-1];
} for(int i = 1; i <= l1; i++) {
for(int j = 1; j <= l2; j++) {
isInterleave[i][j] = (isInterleave[i-1][j] && s1.charAt(i-1) == s3.charAt(i+j-1)) || ((isInterleave[i][j-1]) && s2.charAt(j-1) == s3.charAt(i+j-1));
}
}
return isInterleave[l1][l2];
}
}

LintCode Interleaving String的更多相关文章

  1. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  2. 40. Interleaving String

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

  3. 【leetcode】Interleaving String

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

  4. 二维动态规划——Interleaving String

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

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

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

  6. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  7. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  8. 【LeetCode】97. Interleaving String

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

  9. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

随机推荐

  1. iOS系统网络抓包方法

    转到自己的博客收藏. 1. 网络共享 + 可视化抓包工具 基本原理 原理比较简单,ios设备通过代理方式共享连接mac电脑的无线网卡,使用抓包工具抓包,然后进行分析(我们推荐使用Wireshark,在 ...

  2. 【Python③】python基本数据类型,变量和常量

    基本数据类型 Python中,能直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,包括负整数,程序中的写法和数学上的一样,例如:6,-666,8888…… 计算机使用二进制,所 ...

  3. MySQL5.6 GTID、多线程复制

    MySQL5.6新特性GTID.多线程复制 在Oracle发布MySQL5.6看到众多新特性之后很兴奋,包括对复制的改进.在MySQL5.5半同步复制之后MySQL5.6又引入GTID.多线程复制,在 ...

  4. js判断是否是移动端 访问移动端网址

    1以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.ba ...

  5. protocol的简单写法

    // // TouchView.h // LessonUIControlAndSubClass #import <UIKit/UIKit.h> @class TouchView; //1. ...

  6. OC NSString 基本操作(用到补充持续更新)

    1.将字符串拆分成数组 NSString *string = @"1,2,3,4"; NSArray *array = [string componentsSeparatedByS ...

  7. serialize和unserialize函数

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性.1. serialize和u ...

  8. Calculator(1.5)

    Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...

  9. 配置Java开发IDE

    http://www.cnblogs.com/feichexia/archive/2012/11/07/Vim_JavaIDE.html

  10. C++多线程2

    #include "stdafx.h" #include <windows.h> int g_count; ; DWORD __stdcall Func(LPVOID ...