作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/distinct-subsequences/description/

题目描述

Given a string S and a string T, count the number of distinct subsequences of S which equals T.

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

Example 1:

Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation: As shown below, there are 3 ways you can generate "rabbit" from S.
(The caret symbol ^ means the chosen letters) rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

Example 2:

Input: S = "babgbag", T = "bag"
Output: 5
Explanation: As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters) babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^

题目大意

求S中有多少个子序列等于T。

解题方法

动态规划

这个题一看就是DP。向字符串序列问题确实有很多都是用DP求解的。

设dp数组dp[i][j]表示S的前j个字符是T的前i个字符的子序列的个数为dp[i][j]。

那么有dp[0][*] == 1,因为这个情况下,只能使用s的空字符串进行匹配t。

如果s[j - 1] == t[i - 1],那么,dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1],原因是t的前j个字符可以由s的前[i - 1]个字符和t的前[j - 1]个匹配的同时最后一个字符匹配,加上s的前[j - 1]个字符和t的前[i]个字符匹配同时丢弃s的第[j]个字符。

如果s[j - 1] != t[i - 1],那么dp[i][j] = dp[i][j - 1],因为只能是前面的匹配,最后一个字符不能匹配,所以丢弃了。

class Solution:
def numDistinct(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
M, N = len(s), len(t)
dp = [[0] * (M + 1) for _ in range(N + 1)]
for j in range(M + 1):
dp[0][j] = 1
for i in range(1, N + 1):
for j in range(1, M + 1):
if s[j - 1] == t[i - 1]:
dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]
else:
dp[i][j] = dp[i][j - 1]
return dp[-1][-1]

日期

2018 年 11 月 19 日 —— 周一又开始了

【LeetCode】115. Distinct Subsequences 解题报告(Python)的更多相关文章

  1. Leetcode 115 Distinct Subsequences 解题报告

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

  2. LeetCode: Distinct Subsequences 解题报告

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

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

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

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

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

  7. Leetcode#115 Distinct Subsequences

    原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...

  8. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. 微信小程序扫描普通二维码打开小程序的方法

    很久没有写博客了,之前换了一份工作,很久没有做Android开发了,现在转做前端开发了,记录一下遇到的问题及解决的方法. 最近做微信小程序开发,遇到一个需求,后台管理系统生成的问卷和投票会有一个二维码 ...

  2. idea数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    通过idea操作数据库,进行数据的增加,运行时报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 原因:没有导入mysql-connec ...

  3. Flink(六)【ParameterTool类】

    ParameterTool 工具类 object ParameterToolTest { def main(args: Array[String]): Unit = { val params: Par ...

  4. NuxtJS的AsyncData和Fetch使用详解

    asyncData 简介 asyncData 可以用来在客户端加载 Data 数据之前对其做一些处理,也可以在此发起异步请求,提前设置数据,这样在客户端加载页面的时候,就会直接加载提前渲染好并带有数据 ...

  5. C语言产生随机数(伪)

    C语言的获取随机数的函数为rand(), 可以获得一个非负整数的随机数.要调用rand需要引用头文件stdlib.h.要让随机数限定在一个范围,可以采用模除加加法的方式.要产生随机数r, 其范围为 m ...

  6. Hibernate持久化标志符生成策略

    generator子元素定义持久化标识符的生成策略,为持久化类对应的数据库表的主键找到了赋值方法,HIbernate默认将使用assigned的持久化标识符生成策略.关系型数据库的主键定义方式:(1) ...

  7. Turbine使用

    一.简介 Turbine是聚合服务器发送事件流数据的一个工具,Hystrix的监控中,只能监控单个节点,实际生产中都为集群,因此可以通过Turbine来监控集群下Hystrix的metrics情况 T ...

  8. 【Java 基础】Java日期格式问题

    1. Use SimpleDateFormat to format Date. Watch out, SDF is NOT THREAD-SAFE, it might not be important ...

  9. 用户信息系统_serviceImpl

    package com.hopetesting.service.impl;import com.hopetesting.dao.UserDao;import com.hopetesting.dao.i ...

  10. 阿里巴巴Java开发手册摘要(二)

    MySql数据库 一建表规约 1.表达是与否概念的字段,必须使用is_xxx的命名方式,数据类型是unsigned tinyint(1:是,0否) 正例:表达逻辑删除的字段名is_deleted,1表 ...