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的更多相关文章

  1. 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 ...

  2. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  3. LeetCode python实现题解(持续更新)

    目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...

  4. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. [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 ...

  6. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. php+mysqli预处理技术实现添加、修改及删除多条数据的方法

    本文实例讲述了php+mysqli预处理技术实现添加.修改及删除多条数据的方法.分享给大家供大家参考.具体分析如下: 首先来说说为什么要有预处理(预编译)技术?举个例子:假设要向数据库添加100个用户 ...

  2. DOM解析和优化

    DOM解析 1. css不会阻塞DOM解析(DOM Tree),但会阻塞DOM渲染(css Tree + DOM Tree -> render Tree )2. JS阻塞DOM解析,但浏览器会预 ...

  3. bootstrap之常见组件应用1

    bootstrap中,常见的组件有很多,比如按钮,输入框,导航条,巨幕,面板等.这次根据对bootstrap的一系列学习进行总结. 按钮:button <button type="bu ...

  4. Java EE 学习(6):IDEA + maven + spring 搭建 web(2)- 配置 Spring

    参考:https://my.oschina.net/gaussik/blog/513353 注:此文承接上一文:Java EE 学习(5):IDEA + maven + spring 搭建 web(1 ...

  5. vscode编辑器开发react时,设置使emmet支持自定义组件

    "emmet.triggerExpansionOnTab": true 在vscode用户配置当中配置这个,就可以了

  6. Extension Methods "点"函数方法 扩展方法

    原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...

  7. Docker(一):什么是docker

    Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Linux 基金会,遵从了 ...

  8. socket介绍(webService适用场景)

    1.使用场景         - 不同的移动客户端访问      - 需要访问第三方的项目 2.访问第三方应用的方式      ISO的七层模型  : 物理层.数据链路层.网络层.传输层.表示层.会话 ...

  9. Codeforces Round #441 Div. 2 A B C D

    题目链接 A. Trip for Meal 题意 三个点之间两两有路径,分别长为\(a,b,c\),现在从第一个点出发,走\(n-1\)条边,问总路径最小值. 思路 记起始点相邻的边为\(a,b\), ...

  10. TCP/IP协议详解笔记——ARP协议和RARP协议

    ARP:地址解析协议 对于以太网,数据链路层上是根据48bit的以太网地址来确定目的接口,设备驱动程序从不检查IP数据报中的目的IP地址.ARP协议为IP地址到对应的硬件地址之间提供动态映射. 工作过 ...