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) {
if(s1.length() + s2.length() != s3.length())
return false;
boolean[][] matched= new boolean[s1.length()+1][s2.length()+1];
matched[0][0]= true;
for(int i=1;i<= s1.length(); i++){
if(s3.charAt(i-1) == s1.charAt(i-1))
matched[i][0] = true;
}
for(int j= 1;j<= s2.length();j++){
if(s3.charAt(j-1) == s2.charAt(j-1))
matched[0][j] = true;
}
for(int i=1;i<=s1.length(); i++){
char c1 = s1.charAt(i-1);
for(int j = 1;j<= s2.length();j++){
int i3 = i+ j;
char c2 = s2.charAt(j- 1);
char c3 = s3.charAt(i3 -1);
if(c1 == c3)
matched[i][j] =matched[i][j] || matched[i-1][j];
if( c2== c3)
matched[i][j] = matched[i][j] || matched[i][j-1];
}
}
return matched[s1.length()][s2.length()];
}
}

LintCode 29---交叉字符串的更多相关文章

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

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

  2. LintCode——交叉字符串

    描述:给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例:s1 = "aabcc" s2 = "dbbca" - 当 s3 = &quo ...

  3. 交叉字符串 · Interleaving String

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

  4. hihoCoder2月29日(字符串模拟)

    时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...

  5. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  6. 【LintCode】转换字符串到整数

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

  7. 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators

    backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...

  8. lintcode:strStr 字符串查找

    题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...

  9. LintCode 55 比较字符串

    比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序.   样例 给出 A = "ABC ...

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

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

随机推荐

  1. linux 禁ping

    今天用nmap扫描了局域网的主机,发现几个主机开着好多危险端口,做linux的,对这些安全知识有一点了解.遂用nmap扫描了自己的主机是否存在可利用端口.发现每次nmap都能成功的检测我的主机是ali ...

  2. RobotFramework测试库速查表

    标准库 序号 标准库 说  明 1 Builtin 包含经常需要的关键字,自动导入无需import 2 Dialogs 提供了暂停测试执行和从用户的输入方式 3 Collections 提供一组关键词 ...

  3. [占位符 &nbsp; ]

    在做项目的时候,数据库中的数据会存在空值;这样,我们需要在前台给它加以判断, 如果我们不加以判断也是可行的,我们需要添加一个空白占位符  空白占位符 是个不错的选择,这样我们的页面显示数据的时候就不会 ...

  4. LC 774. Minimize Max Distance to Gas Station 【lock,hard】

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  5. GitHub:Python

    ylbtech-GitHub:Python 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://github.com/python 2.   6 ...

  6. router 配置按需加载对应的组件,配置active

    const routes = [ { path: '/', component: App, children: [ {path: '/index/:type', name: 'index', comp ...

  7. Cookie实战案例代码

    import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.s ...

  8. 学习前端第一天心得体会(初步了解HTML5的新特性以及和HTML的区别)

    一.HTML5是什么? HTML5 是最新的 HTML 标准. HTML5 是专门为承载丰富的 web 内容而设计的,并且无需额外插件. HTML5 拥有新的语义.图形以及多媒体元素. HTML5 提 ...

  9. Ubuntu16.04源

    vim /etc/apt/sources.list # 阿里云deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420. ...

  10. postman提交数组格式方式

    提交数组格式数据,对应的服务器端接收的是@RequestBody 和对应的接收值