-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3333.cpp
More file actions
44 lines (37 loc) · 1.16 KB
/
3333.cpp
File metadata and controls
44 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
static const int MOD = 1e9 + 7;
public:
int possibleStringCount(string word, int k) {
if (word.empty()) return 0;
vector<int> groups;
int count = 1;
for (size_t i = 1; i < word.size(); ++i) {
if (word[i] == word[i - 1]) count++;
else {
groups.push_back(count);
count = 1;
}
}
groups.push_back(count);
long long total = 1;
for (int num : groups)
total = (total * num) % MOD;
if (k <= (int)groups.size()) return total;
vector<int> dp(k, 0);
dp[0] = 1;
for (int num : groups) {
vector<int> newDp(k, 0);
long long sum = 0;
for (int s = 0; s < k; ++s) {
if (s > 0) sum = (sum + dp[s - 1]) % MOD;
if (s > num) sum = (sum - dp[s - num - 1] + MOD) % MOD;
newDp[s] = sum;
}
dp = newDp;
}
long long invalid = 0;
for (int s = groups.size(); s < k; ++s)
invalid = (invalid + dp[s]) % MOD;
return (total - invalid + MOD) % MOD;
}
};