LintCode Interleaving String
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2.
For s1 = "aabcc", s2 = "dbbca"
- When s3 =
"aadbbcbcac", returntrue. - When s3 =
"aadbbbaccc", returnfalse.
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.
- 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.
- 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;
- Initialize the boolean array. interleaving[0][0] = true
- 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的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 二维动态规划——Interleaving String
97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
随机推荐
- DevExpress--xtraTabbedMdiManager控件
因项目需要要实现类似jquery的Tab效果,所以要用到xtraTabbedMdiManager控件 使用xtraTabbedMdiManager一般配合navBarControl(上期已写过) 在工 ...
- 不一样的Android选择器,简单方便,地址日期时间都好用!
前言 Android开发有不少情况下会用到Picker,例如选择 地址.日期.时间. 原生Picker和仿iOS的Picker都是上下或左右滑动到固定区域来选择选项: 显示数量少,如果当前选项距离需要 ...
- Bootstrap学习笔记(二)
这一节笔记主要记录排版内容笔记,其内容包括标题.文本(包括段落.粗斜体.对齐).列表.表格等. 一.标题 在bootstrap中H1-H6与非框架版的区别不大,需要注意的是<small>标 ...
- 为Informix数据库开启事务
1.首先在Informix数据库安装根目录的etc文件夹下找到名为ONCONFIG.on_xxxx的配置文件: 2.打开ONCONFIG.on_xxxx文件,在第409行的位置找到TAPEDEV \\ ...
- RHEL7修改swappiness
linux系统swappiness参数在内存与交换分区间优化 2014-08-14 10:24:19分类: Linux swappiness的值的大小对如何使用swap分区是有着很大的联系的.swap ...
- 第一篇:初识bootstrap
Bootstrap是一款目前非常流行的前端框架,简单的说,就是html,css,javascript的工具集,我们可以用bootstrap搭建出简洁,清新的网站或软件界面,有了bootstrap这个利 ...
- 为什么eclipse中代码提示错误,但是项目目录却不提示错误
public class Aasf { public static void main(String[] args) { System.out.println("");aihfsa ...
- linux密码的更改
找到UTF-8,在后面空格后输入init=/bin/sh 然后CHRL+X启动 进入到这个界面,输入mount -o remount,rw / 再输入touch / .autorelabel ,然后 ...
- 项目组J2ee程序员的标志,你中招没 转载+评论
原文在此 校园级别的程序员的标志: 代码中最多的是嵌套if(null == xxx),还要告诉你,null必须写在前面,我靠. 防止把==写成=,c语言时代常犯的错误.由于null不能做左值,在写=的 ...
- Java重载遇到泛型
今天被问到一个有意思的问题,大家都知道重载的概念吧:一个类中定义同名的方法,参数表不同(参数类型,或者参数个数不通): 但是,如果是下面这个两个方法呢 public static int fn(Lis ...