public static int numDistinct(String S, String T) {
        int m = T.length();
        int n = S.length();
        if (m > n) return 0;    // impossible for subsequence
        int[][] path = new int[m + 1][n + 1];
        for (int k = 0; k <= n; k++)
            path[0][k] = 1;    // initialization

        for (int j = 1; j <= n; j++) {
            for (int i = 1; i <= m; i++) {
                path[i][j] = path[i][j - 1] +
                        (T.charAt(i - 1) == S.charAt(j - 1) ? path[i - 1][j - 1] : 0);
            }
        }

        return path[m][n];
    }