Birdsong recordings
A bird sings the notes of a string S, one note after another. You record exactly M of the notes, keeping their order. Between two recorded notes you may skip at most G notes, and two recorded notes in a row must be different letters. Count the ways to choose which M notes to record, modulo 10^9 + 7.
Reported in: Infosys (SP / DSE · 29 Nov 2026) (memory-based)
Input
First line: S. Second line: M. Third line: G.
Output
The number of ways modulo 10^9 + 7.
Constraints
1 ≤ M ≤ |S| ≤ 2000; 0 ≤ G ≤ |S|; S has lowercase letters.
Example 1
Input:
abca
3
1
Output:
4Example 2
Input:
aab
2
0
Output:
1Example 3
Input:
banana
3
1
Output:
5Hint: Let ways[j][i] be the number of ways to record j notes ending at position i. It is the sum of ways[j − 1][p] for p from i − G − 1 to i − 1, minus the p where S[p] equals S[i]. Prefix sums (one overall, one per letter) make each step O(1), so the whole count is O(M·|S|).