forked from walnutown/CodingInTheDeep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctSubsequence.java
More file actions
81 lines (75 loc) · 2.5 KB
/
Copy pathDistinctSubsequence.java
File metadata and controls
81 lines (75 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
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.
*/
// This quesiton is actually a variant of Subset Sum
// Basic Backtracking
// For each character occurs in T, it can be counted into the subsequence or not.
// time: O(2^n); space: recursive stack
public class Solution {
public int numDistinct(String S, String T) {
if (S==null || T==null)
return 0;
return dfs(S, T, 0, 0);
}
private int dfs(String S, String T, int i, int j){
if (j==T.length())
return 1;
if (i==S.length() && j<T.length())
return 0;
int count = 0;
for (int k=i; k<S.length(); k++){
if (S.charAt(i)!=T.charAt(j))
continue;
count += dfs(S, T, i+1, j+1) + dfs(S, T, i+1, j);
}
return count;
}
}
// Dynamic Programming
// dp[i][j] -- number of subsequences for S[0,i-1] and T[0,j-1]
// time: O(m*n); space: O(m*n)
public class Solution {
public int numDistinct(String S, String T) {
if (S==null || T==null)
return 0;
int M = S.length(), N = T.length();
int[][] dp = new int[M+1][N+1];
dp[0][0] = 1;
for (int i=1; i<=M; i++)
dp[i][0] = 1;
for (int i=1; i<=M; i++){
for (int j=1; j<=N; 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[M][N];
}
}
// 1d DP, remember the conditions of transform 2d DP to 1d DP, and how to transfrom
public class Solution {
public int numDistinct(String S, String T) {
if (S==null || T==null) return 0;
int m=T.length(), n=S.length();
int[] dp = new int[n+1];
for (int j=0; j<=n; j++) dp[j] = 1;
for (int i=1; i<=m; i++){
int prev = dp[0];
dp[0] = 0;
for(int j=1; j<=n; j++){
int tmp = dp[j];
if (T.charAt(i-1) == S.charAt(j-1)) dp[j] = dp[j-1] + prev;
else dp[j] = dp[j-1];
prev = tmp;
}
}
return dp[n];
}
}