diff --git a/solution/0100-0199/0115.Distinct Subsequences/Solution.cpp b/solution/0100-0199/0115.Distinct Subsequences/Solution.cpp new file mode 100644 index 0000000000000..a34e169b70e38 --- /dev/null +++ b/solution/0100-0199/0115.Distinct Subsequences/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int numDistinct(string s, string t) { + int m = s.size(), n = t.size(); + vector<vector<long>> dp(n + 1, vector<long>(m + 1)); + for (int j = 0; j <= m; ++j) dp[0][j] = 1; + for (int i = 1; i <= n; ++i) { + for (int j = 1; j <= m; ++j) { + dp[i][j] = dp[i][j - 1] + (t[i - 1] == s[j - 1] ? dp[i - 1][j - 1] : 0); + } + } + return dp[n][m]; + } +}; \ No newline at end of file