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.
Problem summary: 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.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Two Pointers
Example 1
"alex"
"aaleex"
Example 2
"saeed"
"ssaaedd"
Related Problems
Maximum Matching of Players With Trainers (maximum-matching-of-players-with-trainers)
Step 02
Core Insight
What unlocks the optimal approach
No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #925: Long Pressed Name
class Solution {
public boolean isLongPressedName(String name, String typed) {
int m = name.length(), n = typed.length();
int i = 0, j = 0;
while (i < m && j < n) {
if (name.charAt(i) != typed.charAt(j)) {
return false;
}
int x = i + 1;
while (x < m && name.charAt(x) == name.charAt(i)) {
++x;
}
int y = j + 1;
while (y < n && typed.charAt(y) == typed.charAt(j)) {
++y;
}
if (x - i > y - j) {
return false;
}
i = x;
j = y;
}
return i == m && j == n;
}
}
// Accepted solution for LeetCode #925: Long Pressed Name
func isLongPressedName(name string, typed string) bool {
m, n := len(name), len(typed)
i, j := 0, 0
for i < m && j < n {
if name[i] != typed[j] {
return false
}
x, y := i+1, j+1
for x < m && name[x] == name[i] {
x++
}
for y < n && typed[y] == typed[j] {
y++
}
if x-i > y-j {
return false
}
i, j = x, y
}
return i == m && j == n
}
# Accepted solution for LeetCode #925: Long Pressed Name
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
m, n = len(name), len(typed)
i = j = 0
while i < m and j < n:
if name[i] != typed[j]:
return False
x = i + 1
while x < m and name[x] == name[i]:
x += 1
y = j + 1
while y < n and typed[y] == typed[j]:
y += 1
if x - i > y - j:
return False
i, j = x, y
return i == m and j == n
// Accepted solution for LeetCode #925: Long Pressed Name
impl Solution {
pub fn is_long_pressed_name(name: String, typed: String) -> bool {
let (m, n) = (name.len(), typed.len());
let (mut i, mut j) = (0, 0);
let s: Vec<char> = name.chars().collect();
let t: Vec<char> = typed.chars().collect();
while i < m && j < n {
if s[i] != t[j] {
return false;
}
let mut x = i + 1;
while x < m && s[x] == s[i] {
x += 1;
}
let mut y = j + 1;
while y < n && t[y] == t[j] {
y += 1;
}
if x - i > y - j {
return false;
}
i = x;
j = y;
}
i == m && j == n
}
}
// Accepted solution for LeetCode #925: Long Pressed Name
function isLongPressedName(name: string, typed: string): boolean {
const [m, n] = [name.length, typed.length];
let i = 0;
let j = 0;
while (i < m && j < n) {
if (name[i] !== typed[j]) {
return false;
}
let x = i + 1;
while (x < m && name[x] === name[i]) {
x++;
}
let y = j + 1;
while (y < n && typed[y] === typed[j]) {
y++;
}
if (x - i > y - j) {
return false;
}
i = x;
j = y;
}
return i === m && j === n;
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(n)
Space
O(1)
Approach Breakdown
BRUTE FORCE
O(n²) time
O(1) space
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
TWO POINTERS
O(n) time
O(1) space
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Shortcut: Two converging pointers on sorted data → O(n) time, O(1) space.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
Moving both pointers on every comparison
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.