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. PHP定时备份MySQL,mysqldump语法大全

    几个常用操作: 1.备份 # 只导出表结构 d:/PHP/xampp/mysql/bin/mysqldump -h127.0.0.1 -P3306 -uroot -p123456 snsgou_sns ...

  2. WCF与Web API 区别(应用场景)

    Web api  主要功能: 支持基于Http verb (GET, POST, PUT, DELETE)的CRUD (create, retrieve, update, delete)操作 请求的回 ...

  3. common.css 值得学习的css样式布局

    正常的项目当中,应当有一个common.css,就是把一些常用的样式,写入其中. 然后再结合一些特性的css,构造漂亮的页面. 下面欣赏一些海盗商城的common.css. /***样式初始化***/ ...

  4. 解析Systemtap

    SystemTap 的架构 让我们深入探索 SystemTap 的某些细节,理解它如何在运行的内核中提供动态探针.您还将看到 SystemTap 是如何工作的,从构建进程脚本到在运行的内核中激活脚本. ...

  5. 使用C#WebClient类访问(上传/下载/删除/列出文件目录)由IIS搭建的http文件服务器

    前言 为什么要写这边博文呢?其实,就是使用C#WebClient类访问由IIS搭建的http文件服务器的问题花了我足足两天的时间,因此,有必要写下自己所学到的,同时,也能让广大的博友学习学习一下. 本 ...

  6. ios从相册:摄像头中获取视频

    ios从相册/摄像头中获取视频 如何从相册中获取视频 使用的是一个和获取照片相同的类UIImagePickerController //相册中获取视频 - (IBAction)clickViedoOF ...

  7. 一步一步实现FormsAuthentic验证登录

    本文不讲原理,只讲用法,原理性的东西网上特别多,不过还是会对一些要用到的东西进行解释,不深入讲原理.本文中用的是Vs2012   .net mvc 4.0.原理看这篇文章,看完这个文章绝对受益匪浅. ...

  8. PERFECT NUMBER PROBLEM(思维)

     题目链接:https://nanti.jisuanke.com/t/38220 题目大意:这道题让我们判断给定数字是否为完美数字,并给来完美数字的定义,就是一个整数等于除其自身之外的所有的因子之和. ...

  9. Android 录音获取分贝值的办法

    参考:http://blog.csdn.net/greatpresident/article/details/38402147 public class MediaRecorderDemo { pri ...

  10. leetcode 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...