描述:给出三个字符串: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——交叉字符串的更多相关文章

  1. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

  2. 交叉字符串 · Interleaving String

    [抄题]: 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成.(洗牌) 比如 s1 = "aabcc" s2 = "dbbca" - 当 s3 ...

  3. lintcode :同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  4. lintcode :旋转字符串

    题目: 旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例 对于字符串 "abcdefg". offset=0 => "abcdef ...

  5. Lintcode--006(交叉字符串)

    Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...

  6. LintCode翻转字符串问题 - python实现

    题目描述:试实现一个函数reverseWords,该函数传入参数是一个字符串,返回值是单词间做逆序调整后的字符串(只做单词顺序的调整即可). 例如:传入参数为"the sky is blue ...

  7. LintCode——旋转字符串

    描述:给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例:对于字符串 "abcdefg"     offset=0 => "abcdefg&qu ...

  8. [LintCode]转换字符串到整数

    问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...

  9. Interleaving String,交叉字符串,动态规划

    问题描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Give ...

随机推荐

  1. 【require.js】模块化开发

    一.Require.js及AMD Require.js:是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一. AMD(Asynchronous Module Definit ...

  2. PHPStorm 中配置 XDebug

    1.下载 Xdebug ps : php版本和xdebug版本一定要相对应 如果不知道下载哪个版本,将phpinfo网页的源代码拷贝到https://xdebug.org/wizard.php,然后按 ...

  3. NOIP2018考前抱佛脚——数据结构基础及STL实现

    目录 动态数组 栈 队列 优先队列 动态数组 srand(time(0)); std::vector<int> qwq; for(int i = 1;i <= 10;++i) qwq ...

  4. Emmm,从删库到跑路系列之.......Root权限的重要性

    中午导入了一个.SQL文件,晚上查navicat的数据库死活打不开 错误代码1030,想起来中午导入了mysql数据库的user表,而那里面什么也没有... 默默删掉了data,然后emmm --in ...

  5. 如何弹出QQ临时对话框实现不添加好友在线交谈效果

    如何不添加好友弹出QQ临时对话框实现在线交谈效果,这样的一个需求,我们真的是太需要了,实现起来也很简单,一行代码即可搞定,需要的朋友可以参考下 其实这个很简单,在img我们加入一个a标签,然后 < ...

  6. Jenkins忘记密码解决办法

    1.进入 如果安装的war包,路劲如下: C:\Users\LENOVO\.jenkins\ 2.打开config.xml  ->将useSecurity设置为false 3.进入系统管理的管理 ...

  7. lnmp服务器配置HTTPS

    server { server_name ktsf.weiyou18.com; #listen 80; listen 443; ssl on; ssl_certificate /usr/local/n ...

  8. css实现气泡框效果

    前提:气泡框或者提示框是网页很常见的,实现它的方式有很多,我们以前最常用的就是切图片 然后通过 "定位" 方式 定位到相应的位置,但是用这种方式维护很麻烦,比如设计师想改成另外一种 ...

  9. CentOS7下账号与用户

    /etc/passwd用户记录文件,文件记录着ID和账号与一般身份用户的对应关系 用户至少会有2个ID,用户ID(UserID简UID)和用户组ID(GroupID简GID). /etc/group用 ...

  10. python下载安装搭建

    python官网下载python运行环境(https://www.python.org/downloads/),建议下载稳定版本,不推荐使用最新版本 安装 然后我们打开CMD,在里面输入python, ...