题目来源:

  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. Java实现串口通信的小样例

    用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME ...

  2. 什么是xss盲打

    什么是xss盲打? 盲打仅仅是一种惯称的说法,就是不知道后台不知道有没有xss存在的情况下,不顾一切的输入xss代码在留言啊,feedback啊之类的地方,尽可能多的尝试xss的语句与语句的存在方式, ...

  3. Android TextView drawableLeft 在代码中实现

    方法1 Drawable drawable= getResources().getDrawable(R.drawable.drawable); /// 这一步必须要做,否则不会显示. drawable ...

  4. Linux下安装Oracle的过程和涉及的知识点-系列4

    10.使用rpm安装包 假设本地有现成的相关包,能够直接使用rpm安装.rpm rpm包名,但有时会出现它须要其他包的支持,这时若须要忽略此提示.强行安装,运行rpm -i --force --nod ...

  5. asp.net 使用my97 datepicker实现前后两个日期的范围界定

    说明:日期选择后,前面的日期小于等后面的日期,后面的日期大于等于前面的日期.点点看就知道了:) - 这里将周末日期不可选.代码如下: <html xmlns="http://www.w ...

  6. ToolStripMenuItem控件实现DatagridView行的上下移

    /*--------------行上移------------------*/ 1 private void 上移ToolStripMenuItem_Click(object sender, Even ...

  7. 栈的实现 -- 数据结构与算法的javascript描述 第四章

    栈 :last-in-first-out 栈有自己特殊的规则,只能 后进入的元素 ,最先被推出来,我们只需要模拟这个规则,实现这个规则就好. peek是返回栈顶元素(最后一个进入的). /** * 栈 ...

  8. Top 10 Mapping APIs: Google Maps, Microsoft Bing Maps and MapQuest

    http://www.programmableweb.com/news/top-10-mapping-apis-google-maps-microsoft-bing-maps-and-mapquest ...

  9. [转]PB 基本语句 循环语句

    PB 基本语句一.赋值语句赋值语句用于给变量.对象属性赋值,这是应用程序中使用最频繁的语句,其语法格式为:variablename = expression_r其中:⑴variablename是变量名 ...

  10. shell中常用的特殊字符

    (1) * 代表0到无穷个任意字符 (2)?代表任意一个字符 (3)代表括号内任意一个字符 (4)[ - ] 代表一个范围中的任意一个字符 如[0-9] 即是代表0-9之间的一个数 (5)[^] 反向 ...