Labels

Saturday, February 28, 2015

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.

Naive Way: I came up with DP at the first glance. However, it is hard to write the iteration logic. I decide to put DP away. I turned to a DFS solution which follows a brute force idea. I am sure it works to solve the problem but didn't get passed by OJ. The run time is O(2^n).

 public class Solution {  
   private int sum;  
   public int numDistinct(String S, String T) {  
     sum = 0;  
     dfs(S,T,0,0);  
     return sum;  
   }  
   private void dfs(String S, String T, int i, int j){  
     // end case  
     if(i >= S.length() || j >= T.length()) return;  
     // recursion  
     for(int u = i;u < S.length();u++){  
       if(S.charAt(u) == T.charAt(j)){  
         if(j==T.length()-1) sum++;  
         dfs(S,T,u+1,j+1);  
       }  
     }  
   }  
 }  

Then I turn back to DP. Even though it's hard to figure out how the iteration logic works, I cannot think of other ways. So I stick to DP. I didn't work out the logic, but I work out the code for the logic using listing-> find pattern method. I listed many cases for opt[i][j] with its three highly related opt[i-1][j], opt[i-1][j-1], opt[i][j-1]. I know opt[i][j] is gonna come out from these three sub opts. After finally figure out opt[i][j] = opt[i-1][j] + opt[i-1][j-1] when S[i]==T[j], I turn back to think how the inner logic is. It is as follows:

When S[i] != T[j], we know that opt[i][j] = opt[i-1][j]! Simple but hard to come up with. Just think that since S[i]!=T[j], S[i] is useless in covering T[0....j].

When S[i] == T[j], S[i] matched T[j]! That brings us to if S[0....i-1] and T[0...j-1] are matched pair, S[0...i] and T[0...j] are matched pairs, too. And the value will be kept. Thus, when S[i] == T[j],
opt[i][j] = opt[i-1][j] + opt[i-1][j-1].

The algorithm took O(nm) run time and space.

 public class Solution {  
   public int numDistinct(String S, String T) {  
     // DP  
     int opt[][] = new int[S.length()+1][T.length()+1];  
     // base case  
     for(int i = 0;i <= S.length();i++) opt[i][0] = 1;  
     // iteration  
     for(int i = 1;i <= S.length();i++)  
       for(int j = 1;j <= T.length();j++)  
         opt[i][j] = opt[i-1][j] + (S.charAt(i-1)== T.charAt(j-1)?opt[i-1][j-1]:0);  
     return opt[S.length()][T.length()];  
   }  
 }  

Improved Way: As usual, once the original version of a 2D space DP came out, there is always a way to convert 2D matrix to 1D array once the logic doesn't affect previous rows.

 public class Solution {  
   public int numDistinct(String S, String T) {  
     // DP  
     int opt[] = new int[T.length()+1];  
     // iteration  
     for(int i = 1;i <= S.length();i++){  
       int pre = 1;  
       for(int j = 1;j <= T.length();j++){  
         int temp = opt[j];  
         opt[j] = opt[j] + (S.charAt(i-1)== T.charAt(j-1)?pre:0);  
         pre = temp;  
       }  
     }  
     return opt[T.length()];  
   }  
 }  

No comments:

Post a Comment