[leetcode]Distinct Subsequences

来源:互联网 发布:java数组的长度单位 编辑:程序博客网 时间:2024/05/28 20:19
class Solution {
public:
    int numDistinct(string S, string T) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (T.empty() || S.empty())
            return 0;
        vector<vector<int>> f(T.size(), vector<int>(S.size(), 0));
        for (size_t i = 0; i < T.size(); ++i)
            for (size_t j = 0; j < S.size(); ++j) {
                if (j > 0)
                    f[i][j] = f[i][j - 1];
                if (T[i] == S[j]) {
                    if (i > 0 && j > 0)
                        f[i][j] += f[i - 1][j - 1];
                    else if (i == 0 && j >= 0)
                        f[i][j] += 1;
                }
            }
        return f[T.size() - 1][S.size() - 1];
    }
};
原创粉丝点击