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. Redis5.0 3台主机三主三从部署Cluster集群

    1.下载安装 [root@ip101 redis-5.0.4]# pwd /opt/app/redis-5.0.4 [root@ip101 redis-5.0.4]# ls 00-RELEASENOT ...

  2. JSP——常用配置-获取项目跟路径

    将该内容放入一个空白的JSP文件中,保存为basepath.jsp,然后在需要引用的JSP文件中使用include标签引用. <% String path = request.getContex ...

  3. setHasFixedSize(true)的意义 (转)

    RecyclerView setHasFixedSize(true)的意义 2017年07月07日 16:23:04 阅读数:6831 <span style="font-size:1 ...

  4. mysql之索引 应用于事物 内连接、左(外)连接、右(外)连接

    什么是索引 索引就像是一本书的目录一样,能够快速找到所需要的内容 索引的作用 加快查询速率,降低IO成本加快表与表之间的连接,减少分组和排序时间 索引类型 普通索引:没有唯一性的基本索引 唯一索引:有 ...

  5. WebService:asp.net类库中添加WebService引用出现问题解决方法

    ylbtech-WebService:asp.net类库中添加WebService引用出现问题解决方法 1.返回顶部 1. 在Web项目内添加WebService的引用是件很简单的事情,今天在类库中添 ...

  6. CentOS linux7 重置root密码

    1.启动linux,进入grub,就是选择系统界面,选中系统按e进入编辑界面 2.找到linux16那一行,在末尾加上init=/bin/sh.按Ctrl+x,使用单用户模式启动4.mount -o ...

  7. ORA-00600: internal error code, arguments: [kdBlkCheckError]

    ORA-00600: internal error code, arguments: [kdBlkCheckError] Table of Contents 1. 现象 2. 分析 3. 故障处理 1 ...

  8. [VBA]提取指定字符串后的数字--代码来源Excelhome

    问题:提取check之后的数字 来源:Excelhome Sub 提取数字() Dim regexp As New regexp With regexp .Pattern = "check\ ...

  9. java 抽取 word,pdf 的四种武器

    转自:https://www.ibm.com/developerworks/cn/java/l-java-tips/     感谢作者发布的文章 用 jacob 其实 jacob 是一个 bridag ...

  10. Selenium 2自动化测试实战30(unittest补充)

    unittest补充 1.用例执行的顺序 #test.py #coding:utf-8 from Demo1 import Count import unittest class TestBdd(un ...