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" - 当 ...
随机推荐
- Python 五个常用模块资料 os sys time re built-in
1.os模块 os模块包装了不同操作系统的通用接口,使用户在不同操作系统下,可以使用相同的函数接口,返回相同结构的结果. os.name:返回当前操作系统名称('posix', 'nt', ' ...
- 移动端flex布局 微信和UC的兼容性
请查看以下两个链接 http://www.tuicool.com/articles/Afq6Bzq http://www.sheng00.com/2148.html
- Android源码编译出错解决办法
编译环境:Ubuntu12.04 64位 Android源码:Android 4.3 以下问题是笔者亲自碰到,通过网上查询整合在一起的. 1.error while loading shared li ...
- .netcore跨平台 之 windows上编译,ubuntu上运行
1 下载并安装netcore sdk 下载地址 https://github.com/dotnet/cli 选取合适的版本下载安装即可 打开 CMD ,输入dotnet,出现以下信息说明已安装好 ...
- OpenLDAP安装
参考: http://54im.com/openldap/centos-6-yum-install-openldap-phpldapadmin-tls-%E5%8F%8C%E4%B8%BB%E9%85 ...
- C语言学习1——结构体剖析
一、定义结构体变量的方法 1.1先声明结构体类型在定义变量名 例如: a.声明结构体类型 struct student { int num; char name[20]; char sex; int ...
- 关于lwip移植到ucsos-ii平台的遇到的问题(一)
移植的步骤参照<Day_Day_Up笔记之uCOS-II_LwIP_在_STM32F107_上移植>,<uCOS平台下的LwIP移植笔记>,<嵌入式网络那些事>. ...
- JavaScript之全局变量和隐式全局变量
通过var创建的全局变量(任何函数之外的程序中创建)是不能被删除的. 无var创建的隐式全局变量(无视是否在函数中创建)是能被删除的.
- MySQL 通过semi join 优化子查询
半连接是MySQL 5.6.5引入的,多在子查询exists中使用,对外部row source的每个键值,查找到内部row source匹配的第一个键值后就返回,如果找到就不用再查找内部row sou ...
- Java 数组声明与初始化
引言 学习了好久的java,每次要写数组的声明和初始化代码,总是理不清.最近又碰到了一次这种情况.这次拿出<Thinking In Java>好好总结一翻. 数组声明 对于数组的声明其实都 ...