Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE"
is a subsequence of "ABCDE"
while "AEC"
is not).
Here is an example:
S = "rabbbit"
, T = "rabbit"
Return 3
.
public class Solution { public int numDistinct(String s, String t) { int[][] dp = new int[s.length()+1][t.length()+1]; dp[0][0] = 1; for (int i = 1; i <= s.length(); i++) { dp[i][0] = 1; } for (int i = 1; i <= t.length(); i++) { dp[0][i] = 0; } for (int i = 1; i <= s.length(); i++) { for (int j = 1; j <= t.length(); j++) { dp[i][j] = dp[i-1][j]; if (s.charAt(i-1) == t.charAt(j-1)) { dp[i][j] += dp[i-1][j-1]; } } } return dp[s.length()][t.length()]; } }
相关推荐
《Distinct Subsequences:C++实现解析》 在编程竞赛或算法设计中,"Distinct Subsequences" 是一个常见的问题,它涉及到动态规划(Dynamic Programming)这一重要概念。这道题目要求我们找出一个主串(字符串S)中...
java java_leetcode题解之Distinct Subsequences.java
115.Distinct_Subsequences不同的子序列【LeetCode单题讲解系列】
1.三角形找一条从顶到底的最小路径 2.最大子数组和 3.回文最小划分次数 4.最佳时间买卖股票 5. 判断字符串s3是否由s1,s2交叉存取组成 ...9. 不同的子序列Distinct Subsequences 10.单词分解Word Break
python python_leetcode题解之115_Distinct_Subsequences
javascript js_leetcode题解之115-distinct-subsequences.js
Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态规划-Longest Valid Parentheses](./leetcode/动态规划-Longest Valid Parentheses.java) [动态规划-Maximum Length of Repeated Subarray](./...
合并排序数组, Valid Parentheses, 实现 strStr(), Set Matrix Zeroes, 搜索插入位置, Longest Consecutive Sequence, Valid Palindrome, 螺旋矩阵, 搜索一个二维矩阵, 旋转图像, 三角形, Distinct Subsequences ...
Distinct Subsequences、152. Maximum Product Subarray等。这类题目通常要求读者使用动态规划来解决问题,例如使用DP数组来记录状态、使用递归来计算结果等。 6. 排序和查找类题目 排序和查找类题目是LeetCode原...
10. **Distinct Subsequences** (不同的子序列) 统计一个字符串所有非空子序列在另一个字符串中出现的次数。动态规划方法,考虑当前字符是否出现在子序列中。 11. **Largest Rectangle in Histogram** (柱状图中...
leetcode 分类 LeetCode Progress 128/154 Other Solutions ...Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
115.Distinct Subsequences 2017/04/24 (Lintcode)92.Backpack, (Lintcode)125.Backpack II, (Lintcode)564.Backpack VI 不适用 53.Maximum Subarray, 91.Decode Ways, 96.Unique Binary Search Tree, 120.Triangle,...
leetcode卡 OJ 题目汇总 记录下自己做过的oj题目, 也记录下自己对算法以及编程的理解与...Subsequences两种解法解题失败 [] 2014-06-14-13:00 ~ 2014-06-14-14:40 Interleaving String动态规划解决, Recovering BST失败
java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: ...Subsequences] () [Rotate Image] () [Ugly Number] () [Ugly Number II] () [Repeated DNA Sequences] () [Lar
不同的子序列 给定两个字符串s和t,返回等于t的s的不同子序列数。 字符串的子序列是通过删除某些字符(可以是无字符)而不会干扰其余字符的相对位置而从原始字符串形成的新字符串。 (即,“ ACE”是“ ABCDE”的子...
gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth ...distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
10. leetcode-115-Distinct-Subsequences.md:第115题,不同的子序列,涉及到动态规划和字符串处理。 这些题目覆盖了各种算法和数据结构,包括字符串处理、动态规划、树结构遍历、数组处理、位运算、深度优先搜索、...