LintCode 29---交叉字符串
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---交叉字符串的更多相关文章
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
- LintCode——交叉字符串
描述:给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例:s1 = "aabcc" s2 = "dbbca" - 当 s3 = &quo ...
- 交叉字符串 · Interleaving String
[抄题]: 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成.(洗牌) 比如 s1 = "aabcc" s2 = "dbbca" - 当 s3 ...
- hihoCoder2月29日(字符串模拟)
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- 【LintCode】转换字符串到整数
问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...
- 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators
backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...
- lintcode:strStr 字符串查找
题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...
- LintCode 55 比较字符串
比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序. 样例 给出 A = "ABC ...
- Lintcode--006(交叉字符串)
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...
随机推荐
- koa 基础(十二)koa-static 静态资源中间件 静态web服务
1.目录 2.app.js /** * koa-static 静态资源中间件 静态web服务 * 1.npm install --save koa-static * 2.const static = ...
- [SQL 高级查询运算符的用法 UNION (ALL),EXCEPT(ALL),INTERSECT(ALL) ]
今天看到 三个查询运算符,给大家分享分享 为此我建立了两张表分别为 Articles 和 newArticles 我建立的时候,只建立了一张表 Articles ,表 newArticles 是 ...
- LC 609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- Windows 下的SSH客户端
在日常Linux系统管理中,会使用SSH工具连接服务器,之所以SSH连接主要是为了安全,传统的telnet连接方式是以明文传输,很不安全,网络中如果又热窃听抓包,密码将要泄露.在众多SSH连接中,Pu ...
- java代码实现将集合中的重复元素去掉
package com.loaderman.test; import java.util.ArrayList; import java.util.LinkedHashSet; import java. ...
- Mongdb、Mysql、Redis、Memcache场景
个人的一点理解,不确定一定准确,有不对处欢迎指出 全部数据使用mysql存储,确保安全.准确和持久 大数据.非安全性数据使用Mongodb 小数据.结构丰富.持久化(主从数据)使用redis 小数据. ...
- python的XML解析
http://www.jb51.net/article/63780.htm http://www.runoob.com/python/python-xml.html http://kb.cnblogs ...
- 阶段3 3.SpringMVC·_07.SSM整合案例_05.ssm整合之Spring整合SpringMVC的框架
点击超连接,执行controller里面的方法 那么就需要在Controller里面定义Service对象,就需要依赖注入进来. 启动tomcat服务器,web.xml里面的前端控制器会帮我加载spr ...
- Java语言 List 和 Array 相互转换
Java语言 List 和 Array 相互转换 List集合 转换为 Array数组 List集合 转换成 Array数组,有 2 种方式,代码如下: import java.util.ArrayL ...
- form modelform formset modelformset的各种用法
form modelform formset modelformset的各种用法 首先上结论: form适用于对单个表单的操作,并且需要对每个字段的验证规则自定义. modelform:适用于对用 ...