Short and Clean Java Solution


  • 0
    M
        public int numDistinct(String s, String t) {
            if (s == null || t == null || s.length() < t.length()) return 0;
            int m = s.length(), n = t.length();
            int[][] d = new int[m + 1][n + 1];
            for (int i = 0; i <= m; ++i) d[i][0] = 1;
            for (int i = 1; i <= m; ++i) {
                for (int j = 1; j <= n; ++j) {
                    d[i][j] = d[i - 1][j];
                    if (s.charAt(i - 1) == t.charAt(j - 1)) d[i][j] += d[i - 1][j - 1];
                }
            }
            return d[m][n];
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.