【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 ...
随机推荐
- Unity AngryBots愤怒的机器人demo研究
做为Unity早期的经典demo,一直从3.5以后沿用到4.7.x版本.但其内部一些做法十分不合理.比如使用过多的根目录, 创建怪物和玩家不用SpawnPoint.AI.CheckPoint的代码实现 ...
- Intent 匹配规则
1.在AndroidManifest.xml中可以为 每个 Activity,Service 设置多个Intent-Filter; 在系统启动和程序安装之后,android会收集AndroidMani ...
- jquery总结05-常用事件04-委托事件
委托事件on 多个事件绑定同一个函数 $("#elem").on("mouseover mouseout",function(){ });通过空格分离,传递不同 ...
- javascript 去掉空格之后的字符 正则表达式
从后端数据库读取时间时,经常会把整个日期年月日包括时分秒都取到,如2015-1-28 14:56:00,但是一般的我们只需要前面的年月日就行了.一个简单的方法,直接用split(" &quo ...
- 初试 Matlab 之去除水印
这几天很痛苦地去学习了下用 Matlab 来处理图像,其实那些算法我觉得还不算很难理解,可是 Matlab 这种反人类的语法(可能对于我来说是这样吧,毕竟熟悉了 C++ / Java 的语法一时间很难 ...
- iOS开发 ReactiveCocoa入门教程 第一部分
作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...
- stm32cube--通用定时器--输入捕获
用定时器输入捕获做红外线接收实验.(此次试验以通道2为例) ①stm32cube配置 ② ③ ④程序中主要用到的输入捕获相关寄存器 uint16_t tim_sr,tim_ccer,tim_ccr; ...
- vim 替换操作
全局替换真正使用的是两个ex命令 g(global) 和 :s(switch) 1.替换命令语法 :s/old/new/ 这将把当前模式中的第一个old替换为new 2.替换命令语法 :s/old/n ...
- javaWeb4 http
状态码:服务器处理请求的结果(状态) 常见的状态: 200: 表示请求处理完成并完美返回.ok 302:表示请求需要进一步细化. 404:表示客户访问的资源找不到.Not Found 500:表示服务 ...
- 习题-第5章Web自动化测试
一.选择题 二.判断题 三.填空题 四.简答题 五.设计题