Problem Description
Alice tries to type a specific string but sometimes presses a key for too long so that a character appears multiple times. Given the final output string (word) and a positive integer k, determine the total number of possible original strings Alice might have intended such that the intended string’s length is at least k. Each contiguous block of the same letter in the final output comes from one intended key press that might have produced extra repeated letters. The answer is to be returned modulo 10^9 + 7.
Key Insights
- The final output can be split into groups where each group consists of consecutive identical letters.
- For each group of length n, the intended press could have produced any count from 1 to n.
- The total number of possible original strings (without length restriction) is the product of the possibilities for each group.
- Since the intended string must have a length of at least k, we subtract those combinations where the sum of chosen counts (across groups) is less than k.
- When the number of groups (m) is at least k, the minimum possible length is m which is ≥ k, and therefore every possible combination is valid.
- A dynamic programming (DP) approach using prefix sums efficiently computes, for small m (m < k), the number of ways to achieve a total intended length below k.
Space and Time Complexity
Time Complexity: O(m * k) where m is the number of groups; note that if m ≥ k, no DP is needed. Space Complexity: O(k) for the DP array.
Solution
The solution first parses the given word into groups of consecutive identical characters. For each group with count = n, there are n possibilities (the intended character could have been typed 1 to n times). The overall number for all groups without restriction is the product of these counts (modulo 10^9+7).
However, not every combination is allowed: the intended string length must be at least k. If m (the number of groups) is at least k, then every combination automatically meets the required length. Otherwise, we use DP to count the number of combinations that yield a total length less than k. The DP state dp[s] represents the number of ways to get an intended length sum exactly equal to s from the processed groups. When processing a new group, we update dp using prefix sums to quickly compute the sum over the range of allowable counts (from 1 to the group’s count). Finally, we subtract the invalid combinations (which yield length < k) from the total combinations.
The solution is implemented in multiple languages as shown below.