C++ accepted solution


  • 2
    H
    int numDistinct(string S, string T) {
        
        int m = S.length();
        int n = T.length();
        
        vector<int> sub(n+1, 0);
        sub[0] = 1;
        for (int i = 0; i < m; i++) {
            for (int j = n; j > 0; j--) {
                if (S[i] == T[j-1]) {
                    sub[j] += sub[j-1];
                }
            }
        }
        
        return sub[n];
        //T[i][j] = S[i] == T[j] T[i-1][j-1] + T[i-1][j]
        //          S[i] != T[j] T[i-1][j]
    }

  • 0
    T

    Can you explain a bit?
    How do you solve it so simply?


  • 0
    L

    By simplifying :
    //T[i][j] = S[i] == T[j] T[i-1][j-1] + T[i-1][j]
    // S[i] != T[j] T[i-1][j]
    you may understand


Log in to reply
 

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