-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
54 lines (49 loc) · 1.33 KB
/
Solution.cs
File metadata and controls
54 lines (49 loc) · 1.33 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
45
46
47
48
49
50
51
52
53
54
public class Solution
{
public int PossibleStringCount(string word, int k)
{
int mod = (int)1e9 + 7;
int n = word.Length;
if (n < k) return 0;
if (n == k) return 1;
List<int> freq = [];
int prev = 0;
for (int i = 0; i < n; i++)
{
if (word[i] != word[prev])
{
freq.Add(i - prev);
prev = i;
}
}
freq.Add(n - prev);
long total = 1;
foreach (int val in freq)
{
total = total * val % mod;
}
if (freq.Count >= k) return (int)total;
int need = k - freq.Count;
long[] dp = new long[need];
dp[0] = 1;
for (int i = 0; i < freq.Count; i++)
{
int max = Math.Min(freq[i], need) - 1;
long[] pre = new long[need + 1];
for (int j = 1; j <= need; j++)
{
pre[j] = (pre[j - 1] + dp[j - 1]) % mod;
}
for (int j = 0; j < need; j++)
{
dp[j] = (pre[j + 1] - pre[Math.Max(0, j - max)] + mod) % mod;
}
}
long invalid = 0;
for (int i = 0; i < need; i++)
{
invalid = (invalid + dp[i]) % mod;
}
return (int)((total - invalid + mod) % mod);
}
}