题目来源:

  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. 在mac上访问自带服务器权限问题

    在开发中,有时候我们需要自己的在电脑上做一些网络相关的测试功能,因此,我们必须在本地上模拟网络环境. 在模拟网络环境中,经常会遇到访问权限的问题.现在我就把自己的解决办法写出来.我用的模拟服务器站点是 ...

  2. QT 声明全局变量

    声明 qdatabasemanager.h #include"qdatabasemanager.h" externQDatabaseManager*Database; 引用 mai ...

  3. Myeclipse2014 SVN安装方法以及项目上传到svn服务器

    1. 打开 Myeclipse 工具栏下的Help下的Install from Site 2.打开后弹出窗口, 并点击Add标签,如下图: 3.现在是最重要的一步,填写相关信息. 在对话框Name输入 ...

  4. python文件目录遍历保存成xml文件代码

    Linux服务器有CentOS.Fedora等,都预先安装了Python,版本从2.4到2.5不等,而Windows类型的服务器也多数安装了Python,因此只要在本机写好一个脚本,上传到对应机器,在 ...

  5. Jar包下载地址

    Download Apache log4j 1.2.17下载: http://logging.apache.org/log4j/1.2/download.html jsoup http://jsoup ...

  6. 单选,复选操作div,显示隐藏

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  7. Cglib学习站点(转)

    1.CGlib简单介绍,访问地址:http://www.blogjava.net/stone2083/archive/2008/03/16 /186615.html,从简单的示例到不同业务场景的变化, ...

  8. python中,str和repr的区别

    str函数,它会把值转换为合理形式的字符串,以便用户可以理解. repr会创建一个字符串,它以合法的Python表达式的形式来表示值. 例如: >>> print repr(&quo ...

  9. Django Web开发【1】Django简介

    前言 看完<Django Book>之后, 总想找个实例来实战开发下,无奈国内Django的书籍相当少,只能从英文书籍中吸取养料,偶然之后得到Learning Website Develo ...

  10. 64bits Python2.7.5安装numpy包

    由于数值分析需要numpy计算包,我找了很多numpy-cp27的下载地址,下了最新版的.whl文件,但总是安装不成功,后来找到一个.exe文件 直接下载安装即可使用:下面是网址链接http://do ...