Find the Original Typed String II - Problem

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 10^9 + 7.

Input & Output

Example 1 — Basic Case
$ Input: word = "aabbcc", k = 4
Output: 7
💡 Note: Groups are [2,2,2]. We can shorten each group to length 1 or 2. Valid combinations with length ≥ 4: (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2), (2,2,1), (2,2,2) = 7 ways
Example 2 — Minimum Length
$ Input: word = "aa", k = 1
Output: 2
💡 Note: Group is [2]. We can shorten to length 1 or 2. Both meet k=1 requirement, so 2 ways total
Example 3 — High Requirement
$ Input: word = "aab", k = 5
Output: 0
💡 Note: Groups are [2,1]. Maximum possible length is 2+1=3, which is less than k=5, so 0 ways

Constraints

  • 1 ≤ word.length ≤ 2000
  • word consists of lowercase English letters
  • 1 ≤ k ≤ 2000

Visualization

Tap to expand
Alice's Typing: "aabbcc" → Original Strings ≥ 4 charsaabbccCan be: a or aaCan be: b or bbCan be: c or ccValid Combinations (length ≥ 4):abcc(4), abbcc(5), aabbc(5), abbcc(5), aabcc(5), aabbcc(6), aabbcc(6)Total: 7 different original stringsAnswer: 7
Understanding the Visualization
1
Input
word = "aabbcc", k = 4 (need length ≥ 4)
2
Process
Group consecutive chars: [aa][bb][cc], try shortening each
3
Output
Count valid combinations: 7 ways
Key Takeaway
🎯 Key Insight: Each group of consecutive identical characters can be shortened to any length from 1 to its original size
Asked in
Google 15 Microsoft 12 Amazon 8
12.5K Views
Medium Frequency
~35 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen