Find the Original Typed String II - Problem

Alice is trying to type a specific string, but she's a bit clumsy with her keyboard! ๐Ÿ–ฎ

When Alice types, she sometimes holds keys for too long, causing characters to be repeated multiple times. Given the final string that appears on her screen and a minimum length k, you need to find how many different original strings Alice might have intended to type.

The Challenge: Alice's original string must be at least k characters long. Each group of consecutive identical characters in the typed string could represent 1 or more of that character in the original string.

Example: If Alice typed "aabbcc" and k=3, the original could be "abc", "aabc", "abbc", "abcc", etc.

Return the total count modulo 109 + 7 since the answer can be very large!

Input & Output

example_1.py โ€” Basic Case
$ Input: word = "aabbcc", k = 3
โ€บ Output: 4
๐Ÿ’ก Note: The possible original strings of length โ‰ฅ 3 are: "abc" (length 3), "aabc", "abbc", "abcc" (each length 4). Total: 4 ways.
example_2.py โ€” Single Character
$ Input: word = "aaaa", k = 2
โ€บ Output: 3
๐Ÿ’ก Note: Original could be "aa" (length 2), "aaa" (length 3), or "aaaa" (length 4). All meet k=2 requirement. Total: 3 ways.
example_3.py โ€” Minimum Length Edge Case
$ Input: word = "ab", k = 3
โ€บ Output: 0
๐Ÿ’ก Note: The typed string "ab" has maximum possible original length of 2, but we need k=3. No valid solutions exist.

Constraints

  • 1 โ‰ค word.length โ‰ค 1000
  • 1 โ‰ค k โ‰ค word.length
  • word consists only of lowercase English letters
  • Answer should be returned modulo 109 + 7

Visualization

Tap to expand
๐Ÿ–ฎ Alice's Clumsy Typing ProblemInput: Alice typed "aabbcc", minimum length k=3Question: How many different original strings of length โ‰ฅ 3 could produce this?Group 1: 'aa'Can be 1 or 2 'a'sGroup 2: 'bb'Can be 1 or 2 'b'sGroup 3: 'cc'Can be 1 or 2 'c'sPossible Original Strings (length โ‰ฅ 3):โœ“ "abc" (length 3)โœ“ "aabc" (length 4)โœ“ "abbc" (length 4)โœ“ "abcc" (length 4)โš  "aabbc", "aabcc", "abbcc", "aabbcc" (all > k)๐ŸŽฏ Answer: 4 possible original stringsDP efficiently counts without generating all combinations
Understanding the Visualization
1
Group Consecutive Characters
Identify runs of identical characters: 'aabbcc' โ†’ ['aa', 'bb', 'cc']
2
Calculate Possibilities
Each group can contribute 1 to group_size characters to original
3
Apply Length Constraint
Only count combinations where total length โ‰ฅ k
4
Use DP for Efficiency
Build table to avoid recalculating subproblems
Key Takeaway
๐ŸŽฏ Key Insight: This is a dynamic programming problem where we systematically count combinations of character group sizes that meet the minimum length requirement, avoiding the exponential complexity of generating all possible strings.
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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