115. distinct subsequence leetcode python
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is
a subsequence of "ABCDE" while "AEC" is
not).
Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.
This problem is a typical dp problem.. we need to maintain a dp array to find the result by sequence.
here I have two approaches one I need to use O(m.n) space one need to just use O(m) space.
The time complexity is always the same. O(m.n) because we need to travesal the two strings
the first method is to maintain DP[n][m]
code is as follow
class Solution:
# @return an integer
def numDistinct(self, S, T):
dp=[[0 for j in range(len(T)+1)] for i in range(len(S)+1)]
for i in range(len(S)+1):
dp[i][0]=1
for i in range(1,len(S)+1):
for j in range(1,len(T)+1):
if S[i-1]==T[j-1]:
dp[i][j]=dp[i-1][j-1]+dp[i-1][j]
else:
dp[i][j]=dp[i-1][j]
return dp[-1][-1]
second method saves more space but we need to reversed the order
class Solution:
# @return an integer
def numDistinct(self, S, T):
if len(S)==0:
return 0
if len(T)==0:
return 1###
res=[0 for j in range(len(T)+1)]
res[0]=1
for i in range(len(S)):
for j in reversed(range(len(T))):
if S[i]==T[j]:
res[j+1]=res[j]+res[j+1]
return res[len(T)]
115. distinct subsequence leetcode python的更多相关文章
- Java for LeetCode 115 Distinct Subsequences【HARD】
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- LeetCode python实现题解(持续更新)
目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- [LeetCode] 115. Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
随机推荐
- Spring整合hibernate -hibernateTemplate
目录 1 在Spring中初始化hibernateTemplate并注入Sessionfactory 2 DAO里注入hibernateTemplate 3 getHibernateTemplate. ...
- Mybatis通过接口的方式实现增删改查
导入jar包 [mybatis] [oracle] 生成数据库 1.添加Mybatis的配置文件mybatis-config.xml 在src目录下创建一个mybatis-config.xml文件,如 ...
- 关于Android应用中图片占用内存浅谈
从事过移动端应用开发的童鞋应该都清楚,内存是非常宝贵的资源.如果能很好的利用有限的内存,对应用性能的提升会有很大的帮助.在实际应用开发中图片内存占整个应用非常大的比重,我们只有了解图片是如何加载到内存 ...
- 【bzoj3630】[JLOI2014]镜面通道 对偶图+计算几何+网络流最小割
题目描述 在一个二维平面上,有一个镜面通道,由镜面AC,BD组成,AC,BD长度相等,且都平行于x轴,B位于(0,0).通道中有n个外表面为镜面的光学元件,光学元件α为圆形,光学元件β为矩形(这些元件 ...
- mapserver+openlayers实现左键点击查询
效果图 第一步,配置自己的mapfile,在要查询的图层LAYER对象内加上HEADER,TEMPLATE,FOOTER三个参数,同时,TEMPLATE fooOnlyForWMSGetFeature ...
- POJ 2976 Dropping tests(01分数规划入门)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11367 Accepted: 3962 D ...
- BZOJ3997 [TJOI2015]组合数学 【Dilworth定理】
题目 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走多少 ...
- Linux系统——常见的系统调用
本文列出了大部分常见的Linux系统调用,并附有简要中文说明. 以下是Linux系统调用的一个列表,包含了大部分常用系统调用和由系统调用派生出的的函数.这可能是你在互联网上所能看到的唯一一篇中文注释的 ...
- 企鹅的游戏(penguin)
企鹅的游戏(penguin) 题目描述 Shiva养了一只小企鹅.小企鹅很聪明,她总是帮Shiva和他的好朋友想出很多很好玩的游 戏.其中有一个游戏特别经典,Shiva和他的小伙伴们百玩不厌. 游戏规 ...
- pat 甲级 1065. A+B and C (64bit) (20)
1065. A+B and C (64bit) (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming G ...