[LeetCode]题解(python):097-Interleaving String
题目来源:
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的更多相关文章
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- 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 ...
- LeetCode(97) Interleaving String
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 097 Interleaving String 交错字符串
给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
随机推荐
- 填充Z形二维数组
形如 1 3 4 10 2 5 9 11 6 8 12 15 7 13 14 16 的数组称谓Z形二维数组.填充这样的数组其实只要按照Z形进行行走填充即可,设置一个flag指示方向,行 ...
- Codeforces Round #262 (Div. 2) B
题目: B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes inp ...
- CSS蒙版
蒙版:就是在图片上添加一个图层,用于美化页面,增加页面的可读性 <!DOCTYPE html><html><head lang="en"> &l ...
- Validform表单验证的完美解决方案,推荐给大家
http://validform.rjboy.cn/ 功能简介: 可以在input上直接绑定正则,可以自定义datatype,自定义datatype可以是正则,也可以是函数,datatype可以累加或 ...
- Java 字符串截取函数 substring()
在String中有两个substring()函数,如下: 一:String.substring(int start) 参数: start:要截取位置的索引 返回: 从start开始到结束的字符串 例如 ...
- $.ajax传递字符串到后台,后台返回json对象
var mall = { MallID: $("#createId").val().trim(), MallName: $("#createName").val ...
- CKEditor + CKFinder 实现编辑上传图片配置
下载最新版 ckfinder 本人下载的php版本 https://cksource.com/ckfinder/download 下载最新版ckeditor http://ckeditor.com/ ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- Hadoop插件安装
1.首先下载Hadoop对应版本的插件,以Hadoop 1.0版本对应的插件Hadoop-eclipse-plugin1.0.3.jar为例 2.将下载的插件放置到Ecplise安装目录的plugin ...
- 一周学会Mootools 1.4中文教程:(1)Dom选择器
利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...