Find the Original Typed String I - Problem

Alice is a programmer who's trying to type a specific string on her keyboard, but she's having a rough day! ๐Ÿคฆโ€โ™€๏ธ Sometimes when she presses a key, she accidentally holds it down a bit too long, causing that character to appear multiple times in a row.

Here's the catch: Alice knows she's clumsy, so she's being extra careful. She's confident that at most one character might have been accidentally repeated in her entire typing session.

Given the word that finally appeared on Alice's screen, your task is to determine how many different original strings Alice could have intended to type.

Example: If Alice's screen shows "aab", she could have intended to type either:

  • "aab" (no mistakes)
  • "ab" (the first 'a' was accidentally repeated)

So the answer would be 2 possible original strings.

Input & Output

example_1.py โ€” Basic Case
$ Input: word = "aab"
โ€บ Output: 2
๐Ÿ’ก Note: Alice could have intended "aab" (no mistake) or "ab" (first 'a' was held too long). The consecutive 'aa' gives us one additional possibility.
example_2.py โ€” Multiple Groups
$ Input: word = "aaabbbccc"
โ€บ Output: 4
๐Ÿ’ก Note: Original string plus 3 groups of consecutive characters: "aaa", "bbb", "ccc". Each group represents one way Alice might have made a mistake.
example_3.py โ€” No Duplicates
$ Input: word = "abc"
โ€บ Output: 1
๐Ÿ’ก Note: No consecutive duplicate characters, so Alice couldn't have made any typing mistakes. Only the original string is possible.

Constraints

  • 1 โ‰ค word.length โ‰ค 105
  • word consists of lowercase English letters only
  • Alice made at most one mistake (held one key too long)

Visualization

Tap to expand
Alice's Typing AnalysisInput String: "aabbcc"aabbccGroup 1Group 2Group 3Possible Original Strings:1. "aabbcc" (no mistakes)2. "abbcc" (fixed 'aa' โ†’ 'a')3. "aabcc" (fixed 'bb' โ†’ 'b')4. "aabbc" (fixed 'cc' โ†’ 'c')4TotalPossibilities
Understanding the Visualization
1
Identify Patterns
Look for consecutive identical characters in the typed string
2
Count Possibilities
Each consecutive group could be a mistake - the character might have been intended just once
3
Add Original
Don't forget the possibility that Alice made no mistakes at all
Key Takeaway
๐ŸŽฏ Key Insight: Each group of consecutive identical characters represents exactly one potential typing mistake. The total possibilities equals 1 (original) plus the number of such groups.
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
28.5K Views
Medium Frequency
~12 min Avg. Time
892 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