[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 ...
随机推荐
- Android-IntentFilter
Android-IntentFilter 学习自 <Android开发艺术探索> IntentFilter漫谈 众所周知,在Android中如果要想启动一个Activity,有两种方式,显 ...
- Centos 首次运行MySQL
1:启动MySQL systemctl start mysqld.service 2:查看MySQL运行状态 systemctl status mysqld.service 3:查看默认密码 grep ...
- 洛谷.3381.[模板]最小费用最大流(zkw)
题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...
- hashCode()方法与equals()方法
摘自别人的评论:http://blog.csdn.net/fhm727/article/details/5221792 当向集合Set中增加对象时,首先集合计算要增加对象的hashCode码,根据该值 ...
- Android 开源库获取途径整理
介绍眼下收藏 Android 开源库比較多的 GitHub 项目.站点.Twitter.App 及怎样获取最新的 Android 开源库. 微信号: 1. GitHub Android 开源项目汇总 ...
- OpenOCD 0.9.0 release
OpenOCD 0.9.0 release May 18th, 2015 I’m happy to announce the release of OpenOCD version 0.9.0, fin ...
- dotNetSpider 手记
准备工作: 从github上download工程. 安装VS2017. 安装 .net core 2.0. 编译通过. 基础架构: 调度器 Scheduler 从根site开始,向 Downloade ...
- Golang 处理 Json(一):编码
JSON 是一种数据格式描述语言.以 key 和 value 构成的哈系结构,类似 Javascript 中的对象,python 中的字典.通常 json 格式的 key 是字符串,其值可以是任意类型 ...
- Revit API通过相交过滤器找到与风管相交的对象。
相交过滤器的应用,比几何相交法简便.Excluding剔除 //找到与风管相交的对象,通过相交过滤器. [TransactionAttribute(Autodesk.Revit.Attributes. ...
- firedac调用ORACLE的存储过程
firedac调用ORACLE的存储过程 EMB官方原文地址:http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Using_Oracle_with_F ...