题目来源:

  https://leetcode.com/problems/distinct-subsequences/


题意分析:

  给定字符串S和T,判断S中可以组成多少个T,T是S的子串。


题目思路:

  这是一个动态规划的问题。设定ans[i][j]为s[:i] 组成t[:j]的个数。那么动态规划方程为,if s[i - 1] == t[j - 1]: ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];else: ans[i][j] = ans[i - 1][j]。


代码(python):

  

class Solution(object):
def numDistinct(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
m,n = len(s),len(t)
ans = [[0 for i in range(n+1)] for i in range(m+1)]
for i in range(m + 1):
ans[i][0] = 1
for i in range(1,m + 1):
for j in range(1,n + 1):
if s[i - 1] == t[j - 1]:
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j]
else:
ans[i][j] = ans[i-1][j]
return ans[m][n]

[LeetCode]题解(python):115-Distinct Subsequences的更多相关文章

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

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

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

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

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

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

  5. Leetcode 115 Distinct Subsequences 解题报告

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

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

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

  8. 115. Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  9. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

  10. 115. Distinct Subsequences (String; DP)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果

    ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似.Compo ...

  2. sql server varchar(10)和 nvarchar(10)存储数据长度

    ) 存储10个字母,英文标点符号等,5个汉字以及中文标点等. )存储10汉字.字母等,不区分中英文.

  3. Oracle的字符替换函数translate用法

    参考文档如下:http://www.banping.com/2009/05/18/oracle_function_translate/ Oracle提供了一个字符替换函数translate,不同于re ...

  4. Tomcat使用startup启动,一闪而过,如何查看出错信息

    解决办法:打开startup.bat,通过使用run命令在原有启动窗口显示出错信息. 在startup.bat中找到:call "%EXECUTABLE%" start %CMD_ ...

  5. ROS开发文档

    http://docs.ros.org/indigo/api/roscpp/html/classros_1_1NodeHandle.html#af0bf7baa0ab2385275bb657cc358 ...

  6. hdu 2824The Euler function

    题目链接 快速求出a到b之间所有数的欧拉函数. 一个一个求肯定是不行的, 我们知道欧拉函数的公式为phi(n) = n*(1-1/i1)*(1-1/i2).......i1, i2为素因子. 那么我们 ...

  7. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  8. Cortex-M3动态加载三(模块调用系统函数)

    在我的arm动态加载实验中需要解决一个模块调用系统函数的问题,可以使用以下的一个方法.将系统函数固定在某一段地址空间,然后导出这一块的符号表到符号文件中,要记载的模块link的时候使用这个符号表文件, ...

  9. SMT贴片红胶基本知识

    SMT贴片红胶是一种聚稀化合物,与锡膏不同的是其受热后便固化,其凝固点温度为150℃,这时,红胶开始由膏状体直接变成固体. SMT贴片机装贴贴片具有粘度流动性,温度特性,润湿特性等.根据红胶的这个特性 ...

  10. RHEL6.4 postfix+dovecot搭建邮件服务器

    实验需求:为公司搭建一台能够收信和发信的邮件服务器(192.168.100.1),为员工提供服务,公司域名为jinjianjun.com. 一.修改DNS服务器(192.168.100.2)上mx邮件 ...