[leetcode]Interleaving String @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/
题意:
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc",
s2 = "dbbca",
When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.
解题思路:动态规划。dp[i][j]表示s1[0...i-1]和s2[0...j-1]是否可以拼接为s3[0...i+j-1],可以拼接为true,不可以拼接为false。
代码:
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
if len(s1)+len(s2)!=len(s3): return False
dp=[[False for i in range(len(s2)+1)] for j in range(len(s1)+1)]
dp[0][0]=True
for i in range(1,len(s1)+1):
dp[i][0] = dp[i-1][0] and s3[i-1]==s1[i-1]
for i in range(1,len(s2)+1):
dp[0][i] = dp[0][i-1] and s3[i-1]==s2[i-1]
for i in range(1,len(s1)+1):
for j in range(1,len(s2)+1):
dp[i][j] = (dp[i-1][j] and s1[i-1]==s3[i+j-1]) or (dp[i][j-1] and s2[j-1]==s3[i+j-1])
return dp[len(s1)][len(s2)]
[leetcode]Interleaving String @ Python的更多相关文章
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Interleaving String 解题思路
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- [leetcode]Scramble String @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
随机推荐
- tesseract_ocr+pytesseract图像识别
一.windows安装配置 其他系统安装配置参考github:https://github.com/tesseract-ocr/tesseract/wiki 下载tesseract-ocr参考:htt ...
- hihoCoder.1465.后缀自动机五 重复旋律8(后缀自动机)
题目链接 \(Description\) 给定母串S,求模式串的循环同构串在S中的出现次数. \(Solution\) 将模式串s复制一遍,在母串的SAM上匹配,记录以每个位置作为后缀所能匹配的最大长 ...
- BZOJ.4516.[SDOI2016]生成魔咒(后缀自动机 map)
题目链接 后缀数组做法见这. 直接SAM+map.对于每个节点其产生的不同子串数为len[i]-len[fa[i]]. //15932kb 676ms #include <map> #in ...
- 工具使用-----Jmeter的基础用法
//摘抄至http://www.cnblogs.com/TankXiao/p/4045439.html 以下是我自己录制的关于这篇文章的视频,有兴趣的可以下载哦 https://yunpan.cn/c ...
- BZOJ1170 : [Balkan2007]Cipher
首先对于每个位置,求出它开始长度为y的横行的hash值,然后对于hash值再求一次竖列的hash值,排序后求出众数即可. 时间复杂度$O(n^2\log n)$. #include<cstdio ...
- 浅谈web缓存(转)
这是一篇知识性的文档,主要目的是为了让Web缓存相关概念更容易被开发者理解并应用于实际的应用环境中.为了简要起见,某些实现方面的细节被简化或省略了.如果你更关心细节实现则完全不必耐心看完本文,后面参考 ...
- Centos 安装Percona Toolkit工具集
1.下载 下载地址: https://www.percona.com/downloads/percona-toolkit/LATEST/ [root@bogon ~]# wget https:// ...
- STM32F4 Timer simplified block diagram
Timers TIM1 and TIM8 use 16-bit counters and are the most complex timers of all timers included in t ...
- 【Go入门教程2】基本构成元素:标识符(identifier)、关键字(keyword 25个)、字面量(literal)、分隔符(delimiter)、和 操作符(operator)
基本构成要素 Go 的语言符号 又称 词法元素,共包括 5 类内容——标识符(identifier).关键字(keyword).字面量(literal).分隔符(delimiter) 和 操作符(op ...
- linux内核数据包转发流程(二):中断
[版权声明:转载请保留出处:blog.csdn.net/gentleliu.邮箱:shallnew*163.com] 内核在处理2层数据包之前,必须先处理中断系统.设立中断系统,才有可能每秒处理成千的 ...