Your friend is typing his name
into a keyboard. Sometimes, when typing a character c
, the key might get long pressed, and the character will be typed 1 or more times.
You examine the typed
characters of the keyboard. Return True
if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
Example 1:
Input: name = "alex", typed = "aaleex" Output: true Explanation: 'a' and 'e' in 'alex' were long pressed.
Example 2:
Input: name = "saeed", typed = "ssaaedd" Output: false Explanation: 'e' must have been pressed twice, but it was not in the typed output.
Constraints:
1 <= name.length, typed.length <= 1000
name
and typed
consist of only lowercase English letters.This problem asks whether a typed string could be a long-pressed version of a name string. A long-pressed string means some characters in the name might be repeated multiple times consecutively in the typed string. We solve this efficiently using a two-pointer approach.
Approach:
The core idea is to compare the characters of name
and typed
simultaneously using two pointers, i
and j
, respectively. We proceed as follows:
Initialization: i
and j
start at 0, pointing to the beginning of both strings.
Character Comparison: If name[i]
and typed[j]
are different, it's impossible for typed
to be a long-pressed version of name
, so we return false
.
Character Grouping: If the characters are the same, we find the consecutive occurrences of that character in both strings. We determine x
as the index after the consecutive occurrences of name[i]
in name
, and y
as the index after the consecutive occurrences of typed[j]
in typed
.
Length Check: We compare the lengths of the character groups. If the length of the group in name
(x - i) is greater than the length of the group in typed
(y - j), it means there are not enough repetitions in typed
, so we return false
.
Pointer Update: If the lengths are consistent or the group in typed
is longer, we update i
to x
and j
to y
, effectively moving to the next distinct character.
Termination: The loop continues until one of the pointers reaches the end of its respective string. If both pointers reach the end simultaneously, it means all characters in name
are present (potentially long-pressed) in typed
, and we return true
. Otherwise, if i
hasn't reached the end of name
, it implies typed
is missing characters, so we return false
.
Time Complexity: O(m + n), where m and n are the lengths of name
and typed
, respectively. We iterate through each string at most once.
Space Complexity: O(1). We only use a constant amount of extra space for pointers and variables.
Code (Python):
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
i = j = 0
while i < len(name) and j < len(typed):
if name[i] != typed[j]:
return False
count_name = 1
k = i + 1
while k < len(name) and name[k] == name[i]:
count_name += 1
k += 1
count_typed = 1
l = j + 1
while l < len(typed) and typed[l] == typed[j]:
count_typed += 1
l += 1
if count_typed < count_name:
return False
i = k
j = l
return i == len(name) and j == len(typed)
The code in other languages (Java, C++, Go, TypeScript, Rust) follows the same algorithmic structure, differing only in syntax and specific library functions. The core logic of the two-pointer comparison and group length verification remains consistent across all implementations.