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. Python之基于socket和select模块实现IO多路复用

    '''IO指的是输入输出,一部分指的是文件操作,还有一部分网络传输操作,例如soekct就是其中之一:多路复用指的是利用一种机制,同时使用多个IO,例如同时监听多个文件句柄(socket对象一旦传送或 ...

  2. 【Luogu】P4172水管局长(LCT)

    题目链接 有个结论是x到y的路径上最长边权值等于最小生成树上最长边权值,于是问题转化为最小生成树. 再考虑把问题反过来,删边变成加边. 于是变成动态维护最小生成树,LCT可以做到. #include& ...

  3. css选择器位置和数量技巧

    1. 除去首个元素 li:not(:first-child) li + li li:first-child ~ li 2. 第1-3个元素 li:nth-child(-n+3) 3. 除去第1-3个元 ...

  4. 刷题总结——次小生成树(bzoj1977 最小生成树+倍增)

    题目: Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了.小 P 说,让小 C ...

  5. linux命令 显示文件内容

    通过命令+文件名查看内容.如下命令可以查看.1, cat :由第一行开始显示文件内容:2,tac:从最后一行开始显示,可以看出tac与cat字母顺序相反:3,nl:显示的时候输出行号:4,more:一 ...

  6. 【05】Vue 之 实例详解与生命周期

    Vue的实例是Vue框架的入口,其实也就是前端的ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进行对整个Vue实例生成.编译.挂着. ...

  7. Python连接MySQL数据库操作

    一.创建数据库及表 CREATE DATABASE testdb; USE testdb; CREATE TABLE `tb_user` ( `id` INT (11) NOT NULL AUTO_I ...

  8. 【Visual Studio】“rc.exe”已退出,代码为 5 ("rc.exe" exited with code 5.)

    [解决方案]找到 rc.exe 所在目录,然后 方法1:添加该目录到 VC++ Directories --> Executable Directories中 方法2:添加到系统变量中的Path ...

  9. nginx报404的可能错误

    1.转移地址写错,如root E:software_setup\ftpfile,而你实际的目录地址(比如test.jpg)在ftpfile文件夹里面的img文件夹下,如果直接找ftpfile\test ...

  10. javascript 之 className属性

    Javascript 可以通过className 属性灵活的更改一个标签元素的CSS 类选择器来实现样式的变化. 1.用className 属性修改节点的css类别 代码如下: <html> ...