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. 为PetaPoco添加实体模板

    Brad为我们提供了T4模板,因为公司一直在使用CodeSmith,故为其写了一个CodeSmith的模板,代码如下: <%-- Name:EntityTemplates Author: Des ...

  2. Android first---放置在外存中的文件读取

    照样以登录案例举例: 第一步:界面绘制:跟之前写过的一个案例一样 第二步:逻辑代码 public class MainActivity extends Activity { //定义全局变量    p ...

  3. Python正则表达式学习摘要及资料

    摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2 ...

  4. python 注意事项

    常见错误 #4:  不理解Python的作用域 Python是基于 LEGB 来进行作用于解析的, LEGB 是 Local, Enclosing, Global, Built-in 的缩写.看起来“ ...

  5. Duilib实现类似电脑管家扫描目录效果

    实现原理: 1.后台开线程遍历目录,遍历出一个文件路径在界面上更新显示(通过发消息通知主界面) 2.需要扩展一下Duilib控件,在此我扩展了CLabelUI,重写了PaintText函数 扩展控件的 ...

  6. javascript实现动态侧边栏

    总的来说就是利用 鼠标悬停onmouseover   和  鼠标移除onmouseout 这两个时间来完成的. 首先是HTML 结构 <body> <div id="div ...

  7. SQL Server 查询表的记录数(3种方法,推荐第一种)

    http://blog.csdn.net/smahorse/article/details/8156483 --SQL Server 查询表的记录数 --one: 使用系统表. SELECT obje ...

  8. spinner下拉框组件

    方法一代码如下: <string-array name="city_name"> <item>浙江</item> <item>上海& ...

  9. hdu4352 XHXJ's LIS

    链接 这个题最不好想到的是状态的保存,也没有几亿的数组让你开,怎么保存前面出现了哪些数字. 题意让你求最长上升子序列的长度为k的数字的数目,可以是不连续的,可以保留一个状态栈,栈顶部依次更新,再保证长 ...

  10. DB2表分区删除

    近日,由于部门数据库读库空间过小,提出删除掉两个月之前日志表的分区(数据库分区是按时间月分区),记述如下: 上网搜索资料发现删除表分区大概分这么几步: 1.查询需要删除掉的分区: select t.D ...