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. Canvas与Image互相转换示例以及利用该技术实现微信长按自动识别二维码功能

    现在扫描二维码已经很普遍,微信扫一扫即可,但是如果二维码是在自己的手机上呢?那就要用到微信里的一个功能了,手指长按二维码,会弹出自动识别的选项,点确定就可以看到二维码的内容了.那么怎么通过前端实现这个 ...

  2. hihoCoder offer 收割编程练习赛 83 C 播放列表

    题目 用 $1,2 ,3 \dots, N$ 代表 $N$ 首歌.设想有 $L$ 个格子排成一排,编号 $1$ 到 $L$ .考虑将这些数字挨个填进格子里的情形.假设当前要往第 $i$ 个格子里填一个 ...

  3. MFC设置对话框背景和边框颜色

    对于对话框,只需要重载默认的消息处理函数就行了: // 重载默认的消息处理函数,主要处理WM_MOVE WM_PAINT WM_NCPAINT WM_NCACTIVATE WM_NOTIFY这// 几 ...

  4. django model:auto_now_add 和 auto_now

    创建django的model时,有DateTimeField.DateField和TimeField三种类型可以用来创建日期字段,其值分别对应着datetime().date().time()三中对象 ...

  5. css 两列自适应布局的4种思路

    前面的话 前面已经介绍过css 两列布局中单列定宽单列自适应布局的6种思路的两列布局,而两列自适应布局是指一列由内容撑开,另一列撑满剩余宽度的布局方式.本文将从float.table.flex和gri ...

  6. Java EE学习记录(一)

    话说大家都在说java EE,但是java EE的分层结构如下: 1.数据持久层:主要由一些负责操作POJO(Plain Old Java Object)的类构成,主要负责将数据保存进入数据库; 2. ...

  7. 四、Ubuntu 一些常用命令

    1.锁定root用户 :sudo passwd -l root 2.解锁root用户 :sudo passwd -u root 3.切换身份:su root  或者  su 其他用户名,然后输入密码, ...

  8. go 函数回调

  9. 2018年东北农业大学春季校赛 K wyh的数列【数论/斐波那契数列大数取模/循环节】

    链接:https://www.nowcoder.com/acm/contest/93/K来源:牛客网 题目描述 wyh学长特别喜欢斐波那契数列,F(0)=0,F(1)=1,F(n)=F(n-1)+F( ...

  10. 2018年东北农业大学春季校赛 I wyh的物品【01分数规划/二分】

    链接:https://www.nowcoder.com/acm/contest/93/I来源:牛客网 题目描述 wyh学长现在手里有n个物品,这n个物品的重量和价值都告诉你,然后现在让你从中选取k个, ...