描述:给出三个字符串: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. Python学习--Selenium模块学习(2)

    Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...

  2. WINDOWS SOCKET编程中accept出来的新连接是阻塞还是非阻塞

    实践证明 SOCKET hNewSock=accept(hListenSock) 当hListenSock为阻塞模型时,hNewSock则为阻塞模型 否则 当hListenSock为非阻塞模型时,hN ...

  3. .net core 入坑经验 - 1、await async

    已经有些日子没学习新知识了,心血来潮想试试core有多大变化和跨平台运行 所以现在就开始捣鼓,然而由于是从.net 4.0直接"跃升"到.net core 以及 asp.net m ...

  4. saltstack二次开发(一)

    Saltstack简介 Salt是一个配置管理系统,能够维护预定义状态的远程节点(比如,确保指定的包被安装,指定的服务在运行),一个分布式远程执行系统,用来在远程节点(可以是单个节点,也可以是任意规则 ...

  5. trycatche

    <?phpheader("Content-type: text/html; charset=utf-8"); try{$aaa = 0;if ($aaa == 9) {ech ...

  6. Kubernetes1.91(K8s)安装部署过程(七)--coredns安装

    为了是集群内的服务能使用dns进行服务解析,集群内需要使用dns服务器,可以按照kube官方dns,即kubedns或者其他的dns,比如coredns, 本例中按照的为coredns,按照简单,编辑 ...

  7. Vue登录方式的切换

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  8. POJ3468(线段树区间求和+区间查询)

    https://vjudge.net/contest/66989#problem/C You have N integers, A1, A2, ... , AN. You need to deal w ...

  9. 包学会之浅入浅出 Vue.js:开学篇

    2016年,乃至接下来整个2017年,如果你要问前端技术框架什么最火,那无疑就是前端三巨头:React.Angular.Vue.没错,什么jQuery,seaJs,gulp等都逐渐脱离了热点.面试的时 ...

  10. CUDA2.4-原理之性能优化及浮点运算

    本部分来自于<大规模并行处理器编程实战>第六章.第七章.打算不再看这本书了,准备看<programming massively parallel processors 2nd> ...