Skip to content

Commit d2bad56

Browse files
authored
Create distinct-subsequences-ii.cpp
1 parent cee3ea6 commit d2bad56

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

C++/distinct-subsequences-ii.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int distinctSubseqII(string S) {
7+
static const int M = 1e9 + 7;
8+
vector<int> dp(26);
9+
int result = 0; // sum of dp
10+
for (const auto& c : S) {
11+
tie(result, dp[c - 'a']) =
12+
make_pair(((2 * result) % M - dp[c - 'a'] + 1) % M,
13+
(result + 1) % M);
14+
}
15+
return (result + M) % M;
16+
}
17+
};

0 commit comments

Comments
 (0)