Java Simple Accepted Solution


  • 2
    P
    public class Solution {
        public int numDistinct(String s, String t) {
            int m = s.length(), n = t.length();
            int[][] f = new int[n+1][m+1];
            for (int i = 0; i < m; i++)
                f[0][i] = 1;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (t.charAt(i) == s.charAt(j))
                        f[i+1][j+1] += f[i][j]+f[i+1][j];
                    else
                        f[i+1][j+1] += f[i+1][j];
            return f[n][m];
        }
    }

Log in to reply
 

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