-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3333_leetcode
More file actions
44 lines (39 loc) · 1.13 KB
/
Copy path3333_leetcode
File metadata and controls
44 lines (39 loc) · 1.13 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
#define ll long long
class Solution {
public:
ll md = 1e9 + 7;
int possibleStringCount(string word, int k) {
int n = word.size();
int cnt = 1, total = 1;
vector<int> seg;
for(int i=1;i<n;i++){
if(word[i]==word[i-1]){
cnt++;
}
else{
total = ((ll) total * cnt)%md;
seg.push_back(cnt-1);
cnt = 1;
}
}
total = ((ll) total * cnt)%md;
seg.push_back(cnt-1);
int mnlen = seg.size();
if(k<=mnlen) return total;
k-=mnlen;
vector<ll> dp(k); dp[0] = 1;
for(int x : seg){
vector<ll> pref(k); pref[0] = dp[0];
for(int i=1;i<k;i++){
pref[i] = (pref[i-1] + dp[i])%md;
}
for(int i=0;i<k;i++){
if(i-x-1>=0) dp[i] = (pref[i] - pref[i-x-1] + md)%md;
else dp[i] = pref[i] ;
}
}
int invalid = 0;
for(int x : dp) invalid = (invalid + x)%md;
return (total - invalid + md)%md;
}
};