描述:给出三个字符串: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. ulimit linux文件配置

    文件描述符在形式上是一个非负整数.实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表.当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符.在程序设计中,一 ...

  2. 【转】Java学习---JDK、JRE和JVM的关系

    [原文]https://www.toutiao.com/i6591348937267872269/ 首先是JDK JDK(Java Development Kit) 是 Java 语言的软件开发工具包 ...

  3. 【转】Linux 高级的视角来查看Linux引导过程

    [原文]https://www.toutiao.com/i6594210975480545800/ 1.概述 图 1 是我们在20,000 英尺的高度看到的视图. 当系统首次引导时,或系统被重置时,处 ...

  4. ubuntu各类问题笔记

    ubuntu文本编辑器中文中文乱码问题解决 转载自:http://www.2cto.com/os/201201/117535.html 缺省配置下,用Ubuntu 的文本编辑器(gedit)打开GB1 ...

  5. [luogu T71973]卡常者π酱

    [luogu T71973]卡常者π酱 题意 给定一个长度为 \(n\) 的字符串, 要求将字符串分割为若干段, 每一段要么是一个字符要么是前面几段的并的子串. 如果某一段是一个单独字符, 则产生 \ ...

  6. 【Ansible 文档】【译文】配置文件

    这里说明一下配置文件的内容,原文地址:http://docs.ansible.com/ansible/latest/intro_configuration.html 这个与[Ansible 文档]配置 ...

  7. BZOJ4892:[TJOI2017]dna(hash)

    Description 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S,有这个序列的碱基序列就会表现出喜欢吃藕的性状,但是研究人员发现对碱基序列S,任意修改其中不超过3个碱基,依然能够表 ...

  8. day1-课堂笔记

    venv   执行方法: 1,pycharm执行 2,cmd命令窗口执行:python D:\PyCharmProjects\MyProject\day1.py 回车   java原理: HW.jav ...

  9. 记录因xen而导致lvs,realserver转发activeconn为0

    8.81----vip,keepalived 8.82----realserver 8.83----realserver 8.84----realserver 在8.81上ipvsadm -Ln的时候 ...

  10. rabbitmq安装错误集

    1.安装依赖yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC unixODBC- ...