- 浏览: 183476 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
题目中给定两个字符串s和t,让我们输出从s变化到t有多少种不同的变换方法,要求只能从s中删除字符(可以不删) 不能改变字符原有的顺序。我们用动态规划的思想来做,首先构造一个二维的DP数组,DP[i][j]代表s.substring(0, i + 1) 变换到t.substring(0, j + 1)有多少种变换方法。当s.charAt(i) == t.charAt(j)时,我们可以保留当前字符s.charAt(i),这时有DP[i-1][j-1]种方法;也可以删除s.charAt(i),这时有DP[i - 1][j]种方法;综上所述,当s.charAt(i) == t.charAt(j)时,DP[i][j] = DP[i - 1][j] + DP[i-1][j-1]。如果s.charAt(i) != t.charAt(j)时,我们只能将s.charAt(i)删除,这时DP[i][j] = DP[i - 1][j]。代码如下:
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.
题目中给定两个字符串s和t,让我们输出从s变化到t有多少种不同的变换方法,要求只能从s中删除字符(可以不删) 不能改变字符原有的顺序。我们用动态规划的思想来做,首先构造一个二维的DP数组,DP[i][j]代表s.substring(0, i + 1) 变换到t.substring(0, j + 1)有多少种变换方法。当s.charAt(i) == t.charAt(j)时,我们可以保留当前字符s.charAt(i),这时有DP[i-1][j-1]种方法;也可以删除s.charAt(i),这时有DP[i - 1][j]种方法;综上所述,当s.charAt(i) == t.charAt(j)时,DP[i][j] = DP[i - 1][j] + DP[i-1][j-1]。如果s.charAt(i) != t.charAt(j)时,我们只能将s.charAt(i)删除,这时DP[i][j] = DP[i - 1][j]。代码如下:
public class Solution { public int numDistinct(String s, String t) { int m = s.length(); int n = t.length(); int[][] dp = new int[m + 1][n + 1]; for(int i = 0; i <= m; i++) dp[i][0] = 1; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) { if(s.charAt(i - 1) == t.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; } else { dp[i][j] = dp[i - 1][j]; } } return dp[m][n]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
《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题,不同的子序列,涉及到动态规划和字符串处理。 这些题目覆盖了各种算法和数据结构,包括字符串处理、动态规划、树结构遍历、数组处理、位运算、深度优先搜索、...