[LeetCode] Distinct Subsequences
Well, a dynamic programming problem. Let's first define its state dp[i][j]
to be the number of distinct subsequences of t[0..i - 1]
in s[0..j - 1]
. Then we have the following state equations:
- General case 1:
dp[i][j] = dp[i][j - 1]
ift[i - 1] != s[j - 1]
; - General case 2:
dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1]
ift[i - 1] == s[j - 1]
; - Boundary case 1:
dp[0][j] = 1
for allj
; - Boundary case 2:
dp[i][0] = 0
for all positivei
.
Now let's give brief explanations to the four equations above.
- If
t[i - 1] != s[j - 1]
, the distinct subsequences will not includes[j - 1]
and thus all the number of distinct subsequences will simply be those ins[0..j - 2]
, which corresponds todp[i][j - 1]
; - If
t[i - 1] == s[j - 1]
, the number of distinct subsequences include two parts: those withs[j - 1]
and those without; - An empty string will have exactly one subsequence in any string :-)
- Non-empty string will have no subsequences in an empty string.
Putting these together, we will have the following simple codes (just like translation :-)):
1 class Solution { 2 public: 3 int numDistinct(string s, string t) { 4 int m = t.length(), n = s.length(); 5 vector<vector<int>> dp(m + 1, vector<int> (n + 1, 0)); 6 for (int j = 0; j <= n; j++) dp[0][j] = 1; 7 for (int j = 1; j <= n; j++) 8 for (int i = 1; i <= m; i++) 9 dp[i][j] = dp[i][j - 1] + (t[i - 1] == s[j - 1] ? dp[i - 1][j - 1] : 0); 10 return dp[m][n]; 11 } 12 };
Notice that we keep the whole m*n
matrix simply for dp[i - 1][j - 1]
. So we can simply store that value in a single variable and further optimize the space complexity. The final code is as follows.
1 class Solution { 2 public: 3 int numDistinct(string s, string t) { 4 int m = t.length(), n = s.length(); 5 vector<int> cur(m + 1, 0); 6 cur[0] = 1; 7 for (int j = 1; j <= n; j++) { 8 int pre = 1; 9 for (int i = 1; i <= m; i++) { 10 int temp = cur[i]; 11 cur[i] = cur[i] + (t[i - 1] == s[j - 1] ? pre : 0); 12 pre = temp; 13 } 14 } 15 return cur[m]; 16 } 17 };