【leetcode刷题笔记】Distinct Subsequences

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相同。

用DP求解。建立二维数组dp[i][j]表示S(0,i-1)有多少不同的字串与T(0,j-1)相同。则:

  1. 当T为空串时,S必然有字串和T匹配,所以dp[i][0] = 1;
  2. 当S为空串时,若T不为空串,则S任意字串必然不同于T,所以dp[0][i] = 0(i != 0);
  3. 当S和T都为空串的时候,匹配,所以dp[0][0] = 1;
  4. 其他情况dp[i][j] = dp[i-1][j] + (S[i-1] == T[j-1]? dp[i-1][j-1] : 0);

第4中情况理解如下:

如上图所示,不管T[i]是否等于s[j],S[0,i-1]中和T[0,j]相同的字串一定包含在S[0,i]中,所以dp[i][j] 至少为 dp[i-1][j];

而如果s[i] = T[j],那么在S[0,i-1]中包含的T[0,j-1]加上T[j],就是S[0,i]中等于的T[0,j]的字串,所以如果s[j] = T[i],dp[i][j] = dp[i-1][j] + dp[i-1][j-1];

代码如下:

 1 public class Solution {
 2     public int numDistinct(String S, String T) {
 3         if(S == null || T == null)
 4             return 0;
 5         int m = S.length();
 6         int n = T.length();
 7         int[][] dp = new int[m+1][n+1];
 8
 9         dp[0][0] = 1;
10         for(int i = 1;i <= m;i++)
11             dp[i][0] = 1;
12
13         for(int i= 1;i<=m;i++){
14             for(int j = 1;j <= n;j++){
15                 dp[i][j] = dp[i-1][j];
16                 dp[i][j] += S.charAt(i-1) == T.charAt(j-1)?dp[i-1][j-1]:0;
17             }
18         }
19
20         return dp[m][n];
21     }
22 }

【leetcode刷题笔记】Distinct Subsequences

时间: 2024-11-16 14:14:23

【leetcode刷题笔记】Distinct Subsequences的相关文章

LintCode刷题笔记-- Distinct Subsequences

题目描述: 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 rel

【leetcode刷题笔记】N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

【leetcode刷题笔记】Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

【leetcode刷题笔记】Subsets

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t