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" - 当 ...
随机推荐
- Dependency Scope
Dependency Scope <dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * compile,缺 ...
- The connection to adb is down, and a severe error has occured.问题解决方法小结
遇到了几次这个问题:The connection to adb is down, and a severe error has occured. You must restart adb and Ec ...
- Fragment的onResume
需求:Fragment每次由不可见到可见时的回调. 可能最先想到的是onResume方法,实际使用中Fragment的onResume调用时机与其Activity一致,因此类似与viewPager搭配 ...
- 利用反射模拟一个spring的内部工作原理
这个简单的案例是实行了登录和注册的功能,没有链接数据库. 在bean中id 是唯一的,id和name的区别在于id不能用特殊字符而name可以用特殊字符,比如:/-\.... package com ...
- AngularJS身份验证:Cookies VS Tokens
基于cookie的身份验证:Cookie-Based Authentication 基于token的身份验证:Token-Based Authentication 跨域:cross-domain 说明 ...
- Linux网络编程的一般步骤(1)
一.套接字的地址结构. IPV4套接字地址结构通常也称为"网际套接字地址结构",它以sockaddr_in 命名;POSIX定义如下: #include <stdio.h&g ...
- Go语言http包Form解析之坑
最近正在用Go语言做一个项目,今天在用http包读取客户端发过来的POST数据时遇到了一点小麻烦,就下面这段代码,死活读不到数据: { var body []byte nRead, err := r. ...
- #define用法解析
#define Add(a,b) a+b; 在一般使用的时候是没有问题的,但是如果遇到如: c * Add(a,b) * d 的时候就会出现问题,代数式的本意是a+b然后去和c,d相乘,但是因为使用了 ...
- Consistent hashing —— 一致性哈希
原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...
- 【zz】matlab 均值方差
转自:http://blog.sina.com.cn/s/blog_4936c31d01011v8j.html 1. 均值 Matlab函数:mean >>X=[1,2,3] >&g ...