LintCode——交叉字符串
描述:给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。
样例:s1 = "aabcc" s2 = "dbbca"
- 当 s3 = "aadbbcbcac",返回 true.
- 当 s3 = "aadbbbaccc", 返回 false.
Java
public class Solution {
/**
* @param s1: A string
* @param s2: A string
* @param s3: A string
* @return: Determine whether s3 is formed by interleaving of s1 and s2
*/
public boolean isInterleave(String s1, String s2, String s3){
// write your code here
int s1_len=s1.length();
int s2_len=s2.length();
int s3_len=s3.length();
if(s1_len == 0){
if(s2.equals(s3))
return true;
else
return false;
}
if(s2_len == 0){
if(s1.equals(s3))
return true;
else
return false;
}
int visited[][] = new int[s1_len + 1][s2_len + 1];
visited[0][0]=1;
for(int i = 0;i < s1_len;i++){
if(s1.charAt(i) == s3.charAt(i))
visited[i + 1][0] = 1;
else
break;
}
for(int j = 0;j < s2_len;j++){
if(s2.charAt(j) == s3.charAt(j))
visited[0][j + 1] = 1;
else
break;
}
for(int i = 1;i < visited.length;i++){
for(int j = 1;j < visited[0].length;j++){
int row = i - 1;
int col = j - 1;
if((visited[i][j - 1] == 1 &&
s2.charAt(col) == s3.charAt(row + col + 1)) ||
(visited[i - 1][j] == 1 &&
s1.charAt(row) == s3.charAt(row + col + 1)))
visited[i][j]=1;
}
}
if(visited[visited.length - 1][visited[0].length - 1] == 1)
return true;
return false;
}
}
LintCode——交叉字符串的更多相关文章
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
- 交叉字符串 · Interleaving String
[抄题]: 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成.(洗牌) 比如 s1 = "aabcc" s2 = "dbbca" - 当 s3 ...
- lintcode :同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- lintcode :旋转字符串
题目: 旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例 对于字符串 "abcdefg". offset=0 => "abcdef ...
- Lintcode--006(交叉字符串)
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...
- LintCode翻转字符串问题 - python实现
题目描述:试实现一个函数reverseWords,该函数传入参数是一个字符串,返回值是单词间做逆序调整后的字符串(只做单词顺序的调整即可). 例如:传入参数为"the sky is blue ...
- LintCode——旋转字符串
描述:给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例:对于字符串 "abcdefg" offset=0 => "abcdefg&qu ...
- [LintCode]转换字符串到整数
问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...
- Interleaving String,交叉字符串,动态规划
问题描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Give ...
随机推荐
- replace 用法
orcl中replace()用法: replace:(字符串 | 列):进行替换: 将bqh1表中name列带“小”的字改成“大”: select * from bqh1select a.*,repl ...
- zabbix之运维疑难总结
2.trousers包 zabbix默认情况下要求trousers包的版本是0.3.12版本以上.如果低于这个版本,有可能zabbix-server服务启动不成功.在mysql7.1版本以上可能会使用 ...
- 4星|《门口的野蛮人2》:美国宝万之争专业户KKR公司的疯狂借债收购史
门口的野蛮人2:KKR与资本暴利的崛起(珍藏版) 英文版是1992年出的.主要内容是1977-1998年之间KKR在美国的杠杆收购简史.从KKR创立开始,讲到1990年KKR差点倒闭.国内A股市场上前 ...
- js将时间戳转换成日期格式-陈远波
var timestamp =1539598555000;//时间戳 //时间戳转换成time格式function timestampToTime(timestamp) { var date = ne ...
- ftp 命令全集
FTP的命令行格式为: ftp -v -d -i -n -g [主机名] , 其中 -v 显示远程服务器的所有响应信息: -n 限制ftp的自动登录,即不使用:.n etrc文件: -d 使用调试方式 ...
- es6安装babel包
1.前面下载node.js及安装淘宝镜像可以查看我写的vue.js环境搭建 2.安装完node后,安装babel npm install -g babel-cli 3.检验babel是否安装成功: b ...
- BZOJ 1113 海报 单调栈
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1113 题目大意: N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. ...
- C++虚函数再复习
- apache不能启动LoadModule php5_module modules/ph
apache不能启动LoadModule php5_module modules/php5apache2.dll的问题 主要是版本问题!!有点不爽!! apache不能启动 加入下面两行,apache ...
- Javascript异步编程之setTimeout与setInterval详解分析(一)
Javascript异步编程之setTimeout与setInterval 在谈到异步编程时,本人最主要会从以下三个方面来总结异步编程(注意:特别解释:是总结,本人也是菜鸟,所以总结不好的,请各位大牛 ...