Watch 10 video solutions for Find the Original Typed String II, a hard level problem involving String, Dynamic Programming, Prefix Sum. This walkthrough by codestorywithMIK has 15,935 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and may press a key for too long, resulting in a character being typed multiple times.
You are given a string word, which represents the final output displayed on Alice's screen. You are also given a positive integer k.
Return the total number of possible original strings that Alice might have intended to type, if she was trying to type a string of size at least k.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: word = "aabbccdd", k = 7
Output: 5
Explanation:
The possible strings are: "aabbccdd", "aabbccd", "aabbcdd", "aabccdd", and "abbccdd".
Example 2:
Input: word = "aabbccdd", k = 8
Output: 1
Explanation:
The only possible string is "aabbccdd".
Example 3:
Input: word = "aaabbb", k = 3
Output: 8
Constraints:
1 <= word.length <= 5 * 105word consists only of lowercase English letters.1 <= k <= 2000Problem Overview: You are given a typed string where characters may appear multiple times because of long key presses. Each group of identical characters could represent fewer presses in the original string. The task is to count how many possible original strings could produce the typed result while satisfying a minimum length constraint k.
Approach 1: Dynamic Programming with Prefix Sum Optimization (O(m · k) time, O(k) space)
Start by compressing the typed string into groups of consecutive characters. For example, aaabb becomes counts [3,2]. If a group has length g, the original string could contain between 1 and g copies of that character. Let dp[j] represent the number of ways to build an original string of length j using processed groups. For each group, transition by adding between 1 and g characters. Direct transitions would be expensive, so prefix sums are used to compute range sums efficiently when updating the DP array.
The key observation: the total combinations from all groups is simply the product of their sizes, but some produce strings shorter than k. The DP tracks counts for lengths < k. Subtract these from the total combinations to get the number of valid originals with length at least k. This approach combines dynamic programming with prefix sum range aggregation to keep updates efficient.
Approach 2: Suffix Array Technique (O(n log n) time, O(n) space)
A suffix-array-based method analyzes repeated character structure directly from the string. By constructing a suffix array and LCP (Longest Common Prefix) array, you can efficiently detect stretches where characters repeat and reason about how many valid contractions of the typed string are possible. These repeated segments correspond to the same groups used in the DP solution. Once the repetition boundaries are identified, you compute the number of valid reductions per segment and combine them while enforcing the minimum length constraint.
This technique relies heavily on advanced string processing structures such as suffix arrays and LCP arrays. It is more implementation-heavy but performs well when working with large strings or when repetition structure must be extracted efficiently from raw text.
Recommended for interviews: The dynamic programming solution with prefix sums is the expected approach. It demonstrates strong reasoning about combinatorics and efficient DP transitions. A brute-force enumeration of all possible contractions quickly becomes exponential, so showing the DP formulation and prefix-sum optimization signals solid algorithmic maturity.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming + Prefix Sum | O(m · k) | O(k) | Best general solution when counting valid original lengths with a constraint |
| Suffix Array Technique | O(n log n) | O(n) | Useful when analyzing repetition structure in very large strings |