[LintCode] Distinct Subsequence
Posted Push your limit!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LintCode] Distinct Subsequence相关的知识,希望对你有一定的参考价值。
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).
It\'s guaranteed the answer fits on a 32-bit signed integer.
Given S = "rabbbit"
, T = "rabbit"
, return 3
.
Do it in O(n^2) time and O(n) memory.
O(n^2) memory is also acceptable if you do not know how to optimize memory.
Solution 1. Recursion
For s[0 ~ n - 1] and t[0 ~ m - 1], there are 2 cases.
case 1. s.charAt(n - 1) != t.charAt(m - 1)
This means s.charAt(n - 1) can\'t be used in subsequence of t, so f(s[0 ~ n - 1], t[0 ~ m - 1]) = f(s[0 ~ n - 2], t[0 ~ m - 1]).
case 2. s.charAt(n - 1) == t.charAt(m - 1)
This means s.charAt(n - 1) can be either used or not used in subsequence of t.
If it is used, there are f(s[0 ~ n - 2], t[0 ~ m - 2]) distinct subsequences of t in s.
If it is not used, there are f(s[0 ~ n - 2], t[0 ~ m - 1]) distinct subsequences of t in s.
so f(s[0 ~ n - 1], t[0 ~ m - 1]) = f(s[0 ~ n - 2], t[0 ~ m - 2]) + f(s[0 ~ n - 2], t[0 ~ m - 1]).
Base case 1. if t is an empty string, then there is 1 distinct subseqnce of empty string in s.
Base case 2. if t is longer than s, there is no subsequences of t in s.
It\'s clear by drawing recursive tree, that this solution has overlapping problems.
1 public class DistinctSubsequence {
2 public int getNumOfDistinctSubsequenceRecursion(String s, String t) {
3 if(s == null || t == null) {
4 return 0;
5 }
6 return recursiveHelper(s, s.length() - 1, t, t.length() - 1);
7 }
8 private int recursiveHelper(String s, int endIdx1, String t, int endIdx2) {
9 if(endIdx2 < 0) {
10 return 1;
11 }
12 if(endIdx1 < endIdx2) {
13 return 0;
14 }
15 if(s.charAt(endIdx1) != t.charAt(endIdx2)) {
16 return recursiveHelper(s, endIdx1 - 1, t, endIdx2);
17 }
18 return recursiveHelper(s, endIdx1 - 1, t, endIdx2 - 1) + recursiveHelper(s, endIdx1 - 1, t, endIdx2);
19 }
20 public static void main(String[] args) {
21 String s1 = "rabbbit", t1 = "rabbit";
22 String s2 = "aabbccdd", t2 = "abcd";
23 String s3 = "rabbbitt", t3 = "rabbit";
24 String s4 = "", t4 = "";
25 String s5 = "lin", t5 = "lin";
26 String s6 = "lin", t6 = "ling";
27 DistinctSubsequence test = new DistinctSubsequence();
28 System.out.println(test.getNumOfDistinctSubsequenceRecursion(s1, t1));
29 System.out.println(test.getNumOfDistinctSubsequenceRecursion(s2, t2));
30 System.out.println(test.getNumOfDistinctSubsequenceRecursion(s3, t3));
31 System.out.println(test.getNumOfDistinctSubsequenceRecursion(s4, t4));
32 System.out.println(test.getNumOfDistinctSubsequenceRecursion(s5, t5));
33 System.out.println(test.getNumOfDistinctSubsequenceRecursion(s6, t6));
34 }
35 }
Solution 2. Dynamic Programming
State: dp[i][j]: the number of distinct subsequence of T[0... j - 1] in S[0... i - 1]
Function:
If S.charAt(i - 1) can be used to match the last character of T[0...j - 1], we can either choose to use it or not use it.
If S.charAt(i - 1) can\'t be used to match the last character of T[0...j - 1], we can only choose not to use it.
The above gives us the following state function.
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j], if S.charAt(i - 1) == T.charAt(j - 1);
dp[i][j] = dp[i - 1][j], if S.charAt(i - 1) != T.charAt(j - 1);
Initialization: dp[0][j] = 0, for j from 1 to T.length(); S is empty string and T is not empty string, no subsequence of T in S;
dp[i][0] = 1, for i from 0 to S.length(); T is empty string, there is always 1 subsequence of T in S, regardless if S is empty or not.
Answer: dp[S.length()][T.length()]
1 public class Solution {
2 public int numDistinct(String S, String T) {
3 if(S == null || T == null){
4 return 0;
5 }
6 int n = S.length();
7 int m = T.length();
8 int[][] dp = new int[n + 1][m + 1];
9 for(int j = 0; j <= m; j++){
10 dp[0][j] = 0;
11 }
12 for(int i = 0; i <= n; i++){
13 dp[i][0] = 1;
14 }
15 for(int j = 1; j <= m; j++){
16 for(int i = j; i <= n; i++){
17 if(S.charAt(i - 1) == T.charAt(j - 1)){
18 dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
19 }
20 else{
21 dp[i][j] = dp[i - 1][j];
22 }
23 }
24 }
25 return dp[n][m];
26 }
27 }
Related Problems
以上是关于[LintCode] Distinct Subsequence的主要内容,如果未能解决你的问题,请参考以下文章
LintCode刷题笔记-- Distinct Subsequences