题目来源:

  https://leetcode.com/problems/interleaving-string/


题意分析:

  给定字符串s1,s2,s3,判断s3是否由s1和s2穿插组成。如“abc”由“ac”,“b”组成,而“cba”不是。


题目思路:

  这是一个动态规划问题。令ans[i][j]为s1[:i]和s2[:j]匹配是否成功。那么动态方程是if s1[i - 1] == s3[i + j - 1] 那么ans[i][j] = ans[i][j] or ans[i - 1][j];if s2[j - 1] ==  s3[i + j - 1] 那么ans[i][j] = ans[i][j] or ans[i][j - 1]。


代码(python):

  

class Solution(object):
def isInterleave(self, s1, s2, s3):
"""
:type s1: str
:type s2: str
:type s3: str
:rtype: bool
"""
i,j,k = 0,0,0
m,n,t = len(s1),len(s2),len(s3)
if m + n != t:
return False
ans = [[False for i in range(n+1)] for j in range(m+1)]
ans[0][0] = True
for i in range(1,m+1):
if s1[i - 1] == s3[i - 1]:
ans[i][0] = True
else:
break
for i in range(1,n + 1):
if s2[i - 1] == s3[i -1]:
ans[0][i] = True
else:
break
for i in range(1,m + 1):
for j in range(1,n + 1):
if s1[i - 1] == s3[i + j - 1]:
ans[i][j] = ans[i][j] or ans[i - 1][j]
if s2[j - 1] == s3[i + j - 1]:
ans[i][j] = ans[i][j] or ans[i][j - 1]
return ans[m][n]

[LeetCode]题解(python):097-Interleaving String的更多相关文章

  1. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  2. Java for LeetCode 097 Interleaving String 【HARD】

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

  3. LeetCode(97) Interleaving String

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

  4. LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]

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

  5. 097 Interleaving String 交错字符串

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

  6. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  7. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  8. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  9. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  10. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

随机推荐

  1. 在iOS当中发送电子邮件和短信

    iOS实现发送电子邮件的方法很简单,首先导入MessageUI.framework框架,然后代码如下: #import "RPViewController.h" //添加邮件头文件 ...

  2. 自定义标签体、MVC

    自定义标签 文件以tld结尾,放在webinfo中 标签名 引用类 标签体 继承SimpleTagSupport,复写doTag() getContext(); getjspBody()   invo ...

  3. 达内TTS6.0课件oop_day03

  4. sql server 2005中没有等于等于,高手自行跳过。。

    set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo ALTER TRIGGER [qiandaoTrigger] ON [dbo].[bbsQianDao] AFT ...

  5. 前端模块与CMS结合

    前端模块与CMS结合 在<FIS官方技术群>经常看到一些讨论,这次是 前端组件化与CMS的相关讨论,主要观点来自群里 漂流瓶(张云龙前辈). CMS是运营人员直接操作,我们往往需求各种各样 ...

  6. c# webConfig中的session超时详细设置

    webConfig中的session超时详细设置 我们在webConfig中设置Session超时的时候,如果最后发行的地址是远程服务器,我们很多不是必须的属性并不用设置,如果设之后,倒不能让 ses ...

  7. 怎样在UICollectionView中添加Header和footer

    ---恢复内容开始--- 怎样在UICollectionView中添加Header和footer 转载于http://my.oschina.net/zboy/blog/221525 摘要 来自-htt ...

  8. PHP GUID的生成源码

    <?php function guid(){ if (function_exists('com_create_guid')){ return com_create_guid(); }else{ ...

  9. JSP标签库

    step1: 定义一个标签处理类:改类必须实现SimpleTag接口,在实际中是拓展它的子类SimpleTagSupport.复写doTag方法 public class MyTag extends ...

  10. C++_基础_C与C++的区别2

    内容: (1)C++中的函数 (2)动态内存 (3)引用 (4)类型转换 (5)C++社区对C程序员的建议 1.C++中的函数1.1 函数的重载(1)重载的概念 在同一个作用域中,函数名相同,函数的参 ...