Advertisement
1988coder

115. Distinct Subsequences

Jan 3rd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. /**
  2.  * Using Dynamic Programming (2D Array). See second solution using 1D array.
  3.  *
  4.  * Create a 2D DP array of rows = s.length()+1 and cols = t.length()+1.
  5.  * DP[i+1][j+1] = Number of distinct subsequences of S[0..i] which equals to
  6.  * T[0..j].
  7.  *
  8.  * First Column DP[i][0] = 1. That's because the empty string is a subsequence
  9.  * of any string but only 1 time.
  10.  *
  11.  * First Row DP[0][j] = 0 (except DP[0][0] = 1). This is because an empty string
  12.  * cannot contain a non-empty string as a substring.
  13.  *
  14.  * From here we can easily fill the whole grid:
  15.  *
  16.  * If S[i] != T[j] => Number of distinct subsequences will remain same as the
  17.  * number of subsequences without S[i] character. DP[i][j] = DP[i-1][j].
  18.  *
  19.  * If S[i] == T[j] => Number of distinct subsequences will be the summation of
  20.  * number of subsequences without S[i] & T[j] character and number of
  21.  * subsequences without S[i] character. DP[i][j] = DP[i-1][j-1] + DP[i-1][j].
  22.  *
  23.  * Time Complexity: O(M * N)
  24.  *
  25.  * Space Complexity: O(M * N)
  26.  *
  27.  * M = Length of the string S. N = Length of the string T.
  28.  */
  29. class Solution {
  30.     public int numDistinct(String s, String t) {
  31.         if (s == null || t == null || s.length() < t.length()) {
  32.             return 0;
  33.         }
  34.         int m = s.length();
  35.         int n = t.length();
  36.         if (n == 0) {
  37.             return 1;
  38.         }
  39.  
  40.         int[][] dp = new int[m + 1][n + 1];
  41.         for (int i = 0; i <= m; i++) {
  42.             dp[i][0] = 1;
  43.         }
  44.         for (int i = 1; i <= m; i++) {
  45.             for (int j = 1; j <= n; j++) {
  46.                 if (s.charAt(i - 1) == t.charAt(j - 1)) {
  47.                     dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
  48.                 } else {
  49.                     dp[i][j] = dp[i - 1][j];
  50.                 }
  51.             }
  52.         }
  53.  
  54.         return dp[m][n];
  55.     }
  56. }
  57.  
  58. /**
  59.  * Using Dynamic Programming (1D Array - Space Optimized).
  60.  *
  61.  * Logic Explaination see above solution's comments.
  62.  *
  63.  * Time Complexity: O(M * N)
  64.  *
  65.  * Space Complexity: O(N)
  66.  *
  67.  * M = Length of the string S. N = Length of the string T.
  68.  */
  69. class Solution {
  70.     public int numDistinct(String s, String t) {
  71.         if (s == null || t == null || s.length() < t.length()) {
  72.             return 0;
  73.         }
  74.         int m = s.length();
  75.         int n = t.length();
  76.         if (n == 0) {
  77.             return 1;
  78.         }
  79.  
  80.         int[] dp = new int[n + 1];
  81.         dp[0] = 1;
  82.  
  83.         for (int i = 1; i <= m; i++) {
  84.             for (int j = n; j >= 1; j--) {
  85.                 if (s.charAt(i - 1) == t.charAt(j - 1)) {
  86.                     dp[j] += dp[j - 1];
  87.                 }
  88.             }
  89.         }
  90.  
  91.         return dp[n];
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement