/* 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