class Solution {
public:
int numDistinct(string s, string t) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > mat(s.size() + 1, vector<int>(t.size() + 1));
mat[0][0] = 1;
for (int i = 1; i <= s.size(); ++i)
mat[i][0] = 0;
for (int i = 1; i <= t.size(); ++i)
mat[0][i] = 0;
for (int i = 1; i <= s.size(); ++i)
{
for (int j = 1; j <= t.size(); ++j)
{
if (s[i - 1] == t[j - 1])
{
mat[i][j] = 0;
for (int k = 0; k < i; ++k)
mat[i][j] += mat[k][j - 1];
}
else
mat[i][j] = 0;
}
}
int sum = 0;
for (int i = 0; i <= s.size(); ++i)
sum += mat[i][t.size()];
return sum;
}
};[Leetcode] Distinct Subsequence
最新推荐文章于 2019-05-28 21:52:57 发布
本文介绍了一种使用C++解决字符串匹配问题的方法,通过构建动态规划矩阵来计算子串匹配的数量,适用于需要高效字符串匹配场景。
2万+

被折叠的 条评论
为什么被折叠?



