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 ...
随机推荐
- NoSQL数据库的认识
SQL数据库和NoSQL数据库介绍 什么是SQL数据库? 关系型数据库是依据关系模型来创建的数据库.而所谓的关系模型就是“一对一.一对多.多对多”等关系模型,这是一种二维表格模型,因此一个关系型数据库 ...
- 实现统计 android手机 CPU使用率
# -*- coding:utf-8 -*- ''' Created on Sep 10, 2018 @author: SaShuangYiBing ''' import subprocess imp ...
- bootstrap模态框input不能获取焦点并编辑【转】
Bootstrap模态框时input标签[日期控件也有这样的问题]不能编辑的问题,下面是我的解决方法: 1.将下图中框出来的属性删掉即可: 2.兼容火狐浏览器,笔者在火狐中还是不能编辑,去掉下图属性即 ...
- swift和oc的protocol的成员变量
都需要协议实现者提供具体变量: 否则认为不符合协议. @protocol edddd <NSObject> @property(nonatomic, strong) NSObject *e ...
- unity3D iTween的使用
iTween是一个动画库,作者创建它的目的就是最小的投入实现最大的产出.让你做开发更轻松,用它能够轻松实现各种动画,晃动,旋转,移动,褪色,上色,控制音频等等 到官网看看 http://itwe ...
- QGis C++ 开发之图层分类显示
开发环境:Win10 + VS2010 + Qt 4.8.6 + QGis 2.14.4 简单介绍下如何用C++方式实现QGis中图层分类显示的方法. 要实现图层的分类显示主要会用到QgsCatego ...
- JS添加标签效果
JS添加标签效果 在豆瓣网上添加自己的标签是一种常见的效果,今天也就做了一个简单的demo.由于时间的问题 我不多原理,大家可以试着操作几遍就能明白其中的原理了. JSFiddle的效果如下: 点击我 ...
- CentOS7下账号与用户
/etc/passwd用户记录文件,文件记录着ID和账号与一般身份用户的对应关系 用户至少会有2个ID,用户ID(UserID简UID)和用户组ID(GroupID简GID). /etc/group用 ...
- 理解JavaScript中的作用域链
理解了作用域链,闭包就不难理解了,所以本文主要谈一谈我对作用域链的理解. 关于JavaScript中变量的作用域,全局变量在程序中始终都有定义.局部变量在声明它的函数体内以及其内部所嵌套的函数内始 ...
- php字符串替换的几个函数
strtr() str_replace() substr_replace() preg_replace() strtr()的用法: <?php $str = "test"; ...