Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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.
Example 1:
Input: word = "abbcccc"
Output: 5
Explanation:
The possible strings are: "abbcccc", "abbccc", "abbcc", "abbc", and "abcccc".
Example 2:
Input: word = "abcd"
Output: 1
Explanation:
The only possible string is "abcd".
Example 3:
Input: word = "aaaa"
Output: 4
Constraints:
1 <= word.length <= 100word consists only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"abbcccc"
"abcd"
"aaaa"
keyboard-row)faulty-keyboard)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3330: Find the Original Typed String I
class Solution {
public int possibleStringCount(String word) {
int f = 1;
for (int i = 1; i < word.length(); ++i) {
if (word.charAt(i) == word.charAt(i - 1)) {
++f;
}
}
return f;
}
}
// Accepted solution for LeetCode #3330: Find the Original Typed String I
func possibleStringCount(word string) int {
f := 1
for i := 1; i < len(word); i++ {
if word[i] == word[i-1] {
f++
}
}
return f
}
# Accepted solution for LeetCode #3330: Find the Original Typed String I
class Solution:
def possibleStringCount(self, word: str) -> int:
return 1 + sum(x == y for x, y in pairwise(word))
// Accepted solution for LeetCode #3330: Find the Original Typed String I
impl Solution {
pub fn possible_string_count(word: String) -> i32 {
1 + word.as_bytes().windows(2).filter(|w| w[0] == w[1]).count() as i32
}
}
// Accepted solution for LeetCode #3330: Find the Original Typed String I
function possibleStringCount(word: string): number {
let f = 1;
for (let i = 1; i < word.length; ++i) {
f += word[i] === word[i - 1] ? 1 : 0;
}
return f;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.