Problem Link:

http://oj.leetcode.com/problems/distinct-subsequences/

A classic problem using Dynamic Programming technique.

Let m and n be the length of the strings T and S. Let R[i][j] be the count of distinct subsequence of T[0..i] in S[0..j]. Obviously, R[i][j] = 0 for i > j.

We initialize R[0][..] first: for j = 1..n-1, if S[j] == T[0], then R[0][j] = R[0][j-1] + 1; otherwise R[0][j] = R[0][j-1].

Then we use following recursive function to update R[i][j] bottom-up (from i = 1 to m-1 and j = i to n-1):

  R[i][j] = R[i][j-1] + R[i-1][j-1], if S[j] == T[i]

  R[i][j] = R[i][j-1], otherwise

The python code is as follows.

class Solution:
# @return an integer
def numDistinct(self, S, T):
"""
Suppose two string S[0..n-1] and T[0..m-1], with n >= m
DP method. Let R[i][j] be the count of distinct subsequences of T[0..i] in S[0..j].
Obviously, R[i][j] = 0, for i > j.
Initialization: R[0][j] from j = 0 to n-1
Recursive Function:
R[i][j] = R[i-1][j-1] + R[i][j-1], if T[i] == T[j]
R[i][j] = R[i][j-1], otherwise
"""
n = len(S)
m = len(T)
# Special case
if n < m:
return 0
# Create the 2D array R
R = []
for _ in xrange(m):
R.append([0]*n)
# Initial R[1][0..n-1]
if T[0] == S[0]:
R[0][0] = 1
for j in xrange(1,n):
if T[0] == S[j]:
R[0][j] = R[0][j-1] + 1
else:
R[0][j] = R[0][j-1]
# Update R from i = 1 to m-1, j = 0
for i in xrange(1, m):
for j in xrange(i, n):
if T[i] == S[j]:
R[i][j] = R[i-1][j-1] + R[i][j-1]
else:
R[i][j] = R[i][j-1]
# Return R[m-1][n-1]
return R[m-1][n-1]

【LeetCode OJ】Distinct Subsequences的更多相关文章

  1. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  2. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  3. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  4. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  5. 【LeetCode OJ】Same Tree

    Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...

  6. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

  7. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  8. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  9. 【LeetCode OJ】Maximum Depth of Binary Tree

    Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...

随机推荐

  1. CSS简写指南(转)

    高效的css写法中的一条就是使用简写.通过简写可以让你的CSS文件更小,更易读.而了解CSS属性简写也是前端开发工程师的基本功之一.今天我们系统地总结一下CSS属性的缩写. 1.色彩缩写 色彩的缩写最 ...

  2. Tortoise 下修改服务器路径(Relocate与Switch)

    今天遇到SVN的路径变化,要在客户端修改服务器的下载路径,当初想直接删除重新checkout,后来想着还要重建项目比较麻烦,就找找修改服务器路径的方法.网上基本说的都是右键-->Relocate ...

  3. 打完补丁后测试db_link对SCN的影响

    环境:11.2.0.4.0 升 11.2.0.4.8 后测试 背景:oracle 的db_link会导致实例间SCN同步,SCN增长速度过快则会产生错误: 方案:oracle官方推荐升级版本,但升级之 ...

  4. iOS开发之滤镜的使用技巧(CoreImage)

    一.滤镜的内容和效果是比较多并且复杂的 ,学习滤镜需要技巧 如下: 两个输出语句解决滤镜的属性选择问题: 1.查询效果分类中包含什么效果按住command 点击CIFilter 进入接口文件 找到第1 ...

  5. KindEditor4.1.10,支持粘贴图片(转载!)

    本人扩展了KindEditor4.1.10,使得他能够在Chrome和IE11中直接粘贴复制的图片(比如通过截图工具把图片直接保存在剪切板中),然后调用上传URL上传图片 方法,修改kindedito ...

  6. rpm安装和卸载软件

    1.安装 rpm -i 需要安装的包文件名 举例如下: rpm -i example.rpm 安装 example.rpm 包: rpm -iv example.rpm 安装 example.rpm ...

  7. AxureRP8实战手册(基础11-20)

    本文目录 基础11. 设置文本框输入为密码 基础12. 设置打开选择文件窗口 基础13. 限制文本框输入字符位数 基础14. 设置文本框提示文字 基础15. 设置文本框回车触发事件 基础16. 设置元 ...

  8. 6/13 Sprint2 看板和燃尽图

    部分页面展示

  9. winform界面闪退

    我在登录成功后跳转到主页面的时候,总是会闪退,调试发现调用这个ShowDialog之后,就会触发主页面的FormClosing C# 窗体关闭时可以触发的事件 FormClosing :在窗体关闭时, ...

  10. HTML5的 2D SVG和SVG DOM的学习笔记(2)---SVG动画

    SVG支持动画.可以通过以下几种方法获得动画效果: 使用SVG动画元素.SVG可以描述随时间变化的图形对象,使用不同的动画元素可以定义运动路径,淡入淡出效果和对象的膨胀.收缩.旋转和变换颜色. 使用S ...