题目

交叉字符串

给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。

样例

比如 s1 = "aabcc" s2 = "dbbca"

- 当 s3 = "aadbbcbcac",返回  true.

- 当 s3 = "aadbbbaccc", 返回 false.

挑战

要求时间复杂度为O(n^2)或者更好

解题

交叉比较,不能够AC

参考这里的程序,直接运行原程序,在自己的基础上进行修改运行到78%测试数据的时候还是错了

这里给了几种失败的方法

下面只有选择动态规划来解题了

动态规划矩阵matched[l1][l2]表示s1取l1长度(最后一个字母的pos是l1-1),s2取l2长度(最后一个字母的pos是l2-1),是否能匹配s3的l1+12长度。

那么,我们有

matched[l1][l2] = s1[l1-1] == s3[l1+l2-1] && matched[l1-1][l2] || s2[l2 - 1] == s3[l1+l2-1] && matched[l1][l2-1]

边界条件是,其中一个长度为0,另一个去匹配s3.

Java

public class Solution {
/**
* Determine whether s3 is formed by interleaving of s1 and s2.
* @param s1, s2, s3: As description.
* @return: true or false.
*/
public boolean isInterleave(String s1, String s2, String s3) {
// write your code here
if(s1.length() + s2.length() != s3.length())
return false;
// if(s1.equals("")&& s2.equals(s3))
// return true;
// if(s2.equals("")&&s1.equals(s3))
// return true;
boolean[][] matched= new boolean[s1.length()+1][s2.length()+1];
matched[0][0]= true;
for(int i1=1;i1<= s1.length(); i1++){
if(s3.charAt(i1-1) == s1.charAt(i1-1))
matched[i1][0] = true;
}
for(int i2= 1;i2<= s2.length();i2++){
if(s3.charAt(i2-1) == s2.charAt(i2-1))
matched[0][i2] = true;
}
for(int i1=1;i1<=s1.length(); i1++){
char c1 = s1.charAt(i1-1);
for(int i2 = 1;i2<= s2.length();i2++){
int i3 = i1+ i2;
char c2 = s2.charAt(i2- 1);
char c3 = s3.charAt(i3 -1);
if(c1 == c3)
matched[i1][i2] =matched[i1][i2] || matched[i1-1][i2];
if( c2== c3)
matched[i1][i2] = matched[i1][i2] || matched[i1][i2-1]; }
}
return matched[s1.length()][s2.length()];
}
}

Java Code

Python

class Solution:
"""
@params s1, s2, s3: Three strings as description.
@return: return True if s3 is formed by the interleaving of
s1 and s2 or False if not.
@hint: you can use [[True] * m for i in range (n)] to allocate a n*m matrix.
"""
def isInterleave(self, s1, s2, s3):
# write your code here
len1 = len(s1)
len2 = len(s2)
len3 = len(s3)
if len1+len2 != len3:
return False
# len1 = 2
# len2 = 3
matched = [[False for i in range(len2+1)] for j in range(len1+1)] matched[0][0] = True
# print matched
for i in range(1,len1+1):
if s1[i-1]==s3[i-1]:
matched[i][0] = True
for i in range(1,len2+1):
if s2[i-1]==s3[i-1]:
matched[0][i] = True
for i1 in range(1,len1+1):
for i2 in range(1,len2+1):
i3 = i1 + i2
if s1[i1-1]==s3[i3-1]:
matched[i1][i2] = matched[i1][i2] or matched[i1-1][i2]
if s2[i2-1]==s3[i3-1]:
matched[i1][i2] = matched[i1][i2] or matched[i1][i2-1] return matched[len1][len2]

Python Code

lintcode 中等题:interleaving String 交叉字符串的更多相关文章

  1. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  2. lintcode 中等题: Implement Trie

    题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例   注意 You may assu ...

  3. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

  4. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  5. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

  6. 97. Interleaving String(字符串的交替连接 动态规划)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  7. 【Lintcode】029.Interleaving String

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

  8. 097 Interleaving String 交错字符串

    给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...

  9. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...

随机推荐

  1. 【Qt】Qt之自定义界面(QMessageBox)【转】

    简述 通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget.QDialog.QMainWindow. 大多数窗体的实现都是采用控件堆积来完 ...

  2. 利用dropbox备份vps数据

    在VPS的数据最好定时备份,免得服务器出了什么问题,数据就全丢了.我使用dropbox定时同步wordpress文件夹和数据库信息. 首先下载dropbox ? 1 wget -O dropbox.t ...

  3. [大牛翻译系列]Hadoop(13)MapReduce 性能调优:优化洗牌(shuffle)和排序阶段

    6.4.3 优化洗牌(shuffle)和排序阶段 洗牌和排序阶段都很耗费资源.洗牌需要在map和reduce任务之间传输数据,会导致过大的网络消耗.排序和合并操作的消耗也是很显著的.这一节将介绍一系列 ...

  4. Unity3d 动态批处理的问题

    这段时间做unity3d的优化,主要的入手是减少draw call.    1.代码上主要是把一些零碎的同材质的合并成一个大的mesh.    2.减少不必要的全屏后期处理.把摄像机的renderin ...

  5. jquery ready()的几种实现方法小结

    几种jQuery的ready ()的写法. 1.最常用也是最标准的  $(document).ready(){  });  2.是上面的简写:  $(function(){  })  很奇怪?为什么能 ...

  6. WPF之UseLayoutRounding和SnapsToDevicePixels

    最近在工作中看别的朋友XML代码时,发现SnapsToDevicePixels 属性然后通过查询资料了解其作用 1)UserLayoutRounding为False,导致控件布局相对屏幕若不是整数则不 ...

  7. 第一次比赛的 C题 (好后面才补的....) CodeForces 546B

    Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...

  8. user-agent中的mozilla

    ie说我等不急了,所以user-agent增加 mozilla标识 这篇文章极其好玩:http://nonfu.me/p/8262.html

  9. 【C#】线程池

    将方法排入队列以便执行,并指定包含该方法所用数据的对象.此方法在有线程池线程变得可用时执行. class Program { static void Main(string[] args) { str ...

  10. Cocos2D-x搭建新环境注意事项

    网上资源都说安装Python后, 设置环境变量, 解压Cocos2Dx压缩包就OK, 但运行CppTest还是会报错, 以下是错误解决方案: 1. 错误提示 error LNK1123: failur ...