public static int numDistinct2(String S, String T) {
        int m = T.length();
        int n = S.length();

        int[][] path = new int[m + 1][n + 1];
        for (int i = 0; i <= m; i++)
            path[i][0] = 0;
        for (int i = 0; i <= n; i++)
            path[0][i] = 0;

        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++) {
                if (T.charAt(i-1) == S.charAt(j-1)) {
                    path[i][j] = path[i - 1][j - 1] + 1;
                } else {
                    path[i][j] = Math.max(path[i - 1][j], path[i][j - 1]);
                }
            }
        return path[m][n];
    }