Find the Original Typed String I - 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.

Although Alice tried to focus on her typing, she is aware that she may still have done this at most once.

You are given a string word, which represents the final output displayed on Alice's screen. Return the total number of possible original strings that Alice might have intended to type.

Input & Output

Example 1 — Multiple Groups
$ Input: word = "aabbaa"
Output: 4
💡 Note: Three groups of consecutive duplicates: 'aa', 'bb', 'aa'. Each can be shortened by one character, plus the original string. Total: 1 + 3 = 4
Example 2 — Single Characters
$ Input: word = "abc"
Output: 1
💡 Note: No consecutive duplicate characters, so only the original string 'abc' is possible
Example 3 — One Long Group
$ Input: word = "aaaa"
Output: 2
💡 Note: One group of 4 consecutive 'a's can be shortened to 3 'a's, plus the original. Total: 1 + 1 = 2

Constraints

  • 1 ≤ word.length ≤ 100
  • word consists of lowercase English letters

Visualization

Tap to expand
Find the Original Typed StringAlice typed: aabbaa (but may have held keys too long)aabbaaPossible original strings:1. aabbaa (what Alice typed)2. abbaa (first 'a' group shortened)3. aabaa ('b' group shortened)4. aabba (last 'a' group shortened)Total: 4 possible original strings
Understanding the Visualization
1
Alice's Output
The final typed string 'aabbaa' displayed on screen
2
Identify Mistakes
Find groups where Alice might have held keys too long
3
Count Possibilities
Each group adds one potential original string
Key Takeaway
🎯 Key Insight: Each consecutive duplicate group represents one potential typing mistake that can be 'undone'
Asked in
Google 15 Microsoft 12 Amazon 8
8.5K Views
Medium Frequency
~15 min Avg. Time
420 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