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" - 当 ...
随机推荐
- Pythonn 内置函数
abs 绝对值 n = abs(-1) print(n) ========================= /usr/bin/python3.5 /home/liangml/pythonscript ...
- Java线程池使用说明
Java线程池使用说明 转自:http://blog.csdn.net/sd0902/article/details/8395677 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极 ...
- .Net模拟提交表单
2016-09-0210:49:20 以中邮速递API为服务接口,由于提交方式为表单提交,我要获取返回值来处理其他业务,所以一开始尝试采用Js后台获取返回值,但是涉及到跨域请求限制问题,那边服务端接口 ...
- google closure--继承模块二:goog.base()demo分析
昨天已经讲到了goog.inherits(),主要负责通过为子构造函数原型对象通过原型链继承父构造函数的原型对象的方法,完成继承.这样继承只完成了原型对象的继承,看看之前的那张图: 是不是感觉父构造函 ...
- C++中的文件读取结束
while(cin>>N>>M) { } ok???
- Oracle 数据库--一个用户同步的sql
用户同步的sql: insert into crm_customer_user ,username,,,,,id, from sys_user where username not in (selec ...
- 安装R语言扩展包vegan
这周的作业我开始得好迟啊...然而还是要努力做啊... ××××××××××××××我是萌萌哒分割线×××××××××××××××××××××××××××××××××××× 首先,百度进入官方页面,看 ...
- array_filter函数
利用array_filter函数轻松去掉多维空值,而数组的下标没有改变, 如果自定义过滤函数返回 true,则被操作的数组的当前值就会被包含在返回的结果数组中, 并将结果组成一个新的数组.如果原数组是 ...
- UGUI与DOtween的坑
在使用ugui和dotween做动画时,如使用transform.DoMoveX,.DoLocalMoveX,.DoMove,.DoLocalMove等方法时,动画效果有可能是错误的,什么时候错误呢? ...
- NYOJY 491 幸运三角形
描述 话说有这么一个图形,只有两种符号组成(‘+’或者‘-’),图形的最上层有n个符号,往下个数依次减一,形成倒置的金字塔形状,除第一层外(第一层为所有可能情况),每层形状都由上层决定,相邻的符号相同 ...