1. public class Solution {
  2. /**
  3. * @param s1: A string
  4. * @param s2: A string
  5. * @param s3: A string
  6. * @return: Determine whether s3 is formed by interleaving of s1 and s2
  7. */
  8. public boolean isInterleave(String s1, String s2, String s3) {
  9. if(s1.length() + s2.length() != s3.length())
  10. return false;
  11. boolean[][] matched= new boolean[s1.length()+1][s2.length()+1];
  12. matched[0][0]= true;
  13. for(int i=1;i<= s1.length(); i++){
  14. if(s3.charAt(i-1) == s1.charAt(i-1))
  15. matched[i][0] = true;
  16. }
  17. for(int j= 1;j<= s2.length();j++){
  18. if(s3.charAt(j-1) == s2.charAt(j-1))
  19. matched[0][j] = true;
  20. }
  21. for(int i=1;i<=s1.length(); i++){
  22. char c1 = s1.charAt(i-1);
  23. for(int j = 1;j<= s2.length();j++){
  24. int i3 = i+ j;
  25. char c2 = s2.charAt(j- 1);
  26. char c3 = s3.charAt(i3 -1);
  27. if(c1 == c3)
  28. matched[i][j] =matched[i][j] || matched[i-1][j];
  29. if( c2== c3)
  30. matched[i][j] = matched[i][j] || matched[i][j-1];
  31. }
  32. }
  33. return matched[s1.length()][s2.length()];
  34. }
  35. }

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. ILSpy C# language support status

    C# language support status Asynchronous methods 已经支持 Generalized async return types  还不支持 Async main ...

  2. centos6一键安装WordPress

    #!/bin/bash service iptables stop setenforce yum -y install httpd service httpd restart yum -y insta ...

  3. Android jni/ndk编程二:jni数据类型转换(primitive,String,array)

    一.数据类型映射概述 从我们开始jni编程起,就不可能避开函数的参数与返回值的问题.java语言的数据类型和c/c++有很多不同的地方,所以我们必须考虑当在java层调用c/c++函数时,怎么正确的把 ...

  4. 【React自制全家桶】三、React使用ref操作DOM与setState遇到的问题

    在React中同时使用ref操作DOM与setState常常会遇到 比如操作的DOM是setState更新之前的DOM内容,与想要的操作不一致.导致这样的原因是setState函数是异步函数. 就是当 ...

  5. C代码输出日志

    模板代码,在实际开发中可以使用: Android.mk文件增加(放到 include $(CLEAR_VARS)下面) LOCAL_LDLIBS += -llog C代码中增加 #include &l ...

  6. 必会SQL笔试题

    ()表名:购物信息 购物人 商品名称 数量 A 甲 B 乙 C 丙 A 丁 B 丙 …… 给出所有购入商品为两种或两种以上的购物人记录 答:); ()表名:成绩表 姓名 课程 分数 张三 语文 张三 ...

  7. spring boot系列(五)spring boot 配置spring data jpa (查询方法)

    接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...

  8. DESC加密解密算法

    using System; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; usi ...

  9. 网络实验 04-利用三层交换机实现VLAN间路由

    利用三层交换机实现VLAN间路由 一.实验目标 掌握交换机Tag VLAN 的配置 掌握三层交换机基本配置方法 掌握三层交换机VLAN路由的配置方法 通过三层交换机实现VLAN间相互通信 二.实验背景 ...

  10. CentOS 7 替换网易yum 源

    首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...