【LeetCode OJ】Distinct Subsequences
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的更多相关文章
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
随机推荐
- python(三)set集合
set集合的特点是无序.不重复序列 创建集合: 1 2 3 4 5 6 7 8 9 10 11 12 13 a.s1 = {11,22} b.s2 = set() c.s3 = set([11,22, ...
- Android 性能分析工具dumpsys的使用(自己增加一部分在后面)
Android提供的dumpsys工具可以用于查看感兴趣的系统服务信息与状态,手机连接电脑后可以直接命令行执行adb shell dumpsys 查看所有支持的Service但是这样输出的太多,可以通 ...
- Bootstrap_按钮工具栏
单个按钮在Web页面中的运用有时候并不能满足我们的业务需求,常常会看到将多个按钮组合在一起使用,比如富文本编辑器里的一组小图标按钮等. Bootstrap框架为大家提供了按钮组组件. <div ...
- jquery之replaceAll(),replaceWith()方法详解
一:replaceAll() replaceAll()函数用于使用当前匹配元素替换掉所有的目标元素. 该函数属于jQuery对象(实例). 语法 jQuery 1.2 新增该函数. jQueryObj ...
- redis windows下使用及redis命令
出自:http://www.cnblogs.com/chenping-987123/archive/2012/01/29/2331079.html Redis 是一个开源,高级的键值对的存储.它经常作 ...
- 简单SSM配置详解
SSM:spring+springMVC+Mybatis 学习网友的http://www.cnblogs.com/invban/p/5133257.html,并对其进行了详细的解说. 源码下载:htt ...
- Shell脚本查看apk签名信息
用shell写了一个查看apk签名的脚本.代码很少也很简单 支持递归目录查询 #!/bin/bash #使用方法 ./getcertificate.sh xx.apk get_signature() ...
- CSS的压缩 方法与解压
为什么要压缩CSS? 1.大网站节约流量 2.加快访问速度 工具:Dreamweaver(手工替换,个人感觉任何文本编辑器都可以)不过DW可以还原 CSS压缩与CSS代码压缩还原方法,CSS压缩工具有 ...
- Window 常用文件
*.msm ntwdblib.dll是一款用于PHP连接MSSQL2005或2008的驱动文件,如果连接BDE的时候出现“cannot load an idapi service liabray fi ...
- 初学者用div+css结构写网页的几个误区
1.用div+css结构制作静态html网页不等于彻底抛弃古老的table写法.之所以不建议用table来布局网页是因为在网页加载很慢的时候要等table结构加载完成才能看到网页,其次是table的布 ...