Leetcode 925: Long Pressed Name

grid47
grid47
Exploring patterns and algorithms
Aug 6, 2024 5 min read

Your friend is typing their name on a keyboard. Sometimes, while typing, a key might be long pressed, causing the character to be typed multiple times. Given the name and the typed string, determine if it is possible that the typed string could have been the result of long pressing some characters while typing the name.
Problem
Approach
Steps
Complexity
Input: You are given two strings: 'name' representing your friend's name and 'typed' representing the characters typed on the keyboard.
Example: Input: name = "emma", typed = "eemmaa"
Constraints:
• 1 <= name.length, typed.length <= 1000
• Both 'name' and 'typed' consist of only lowercase English letters.
Output: Return True if the 'typed' string could have been the result of long pressing characters from the 'name'. Otherwise, return False.
Example: Output: True
Constraints:
• The strings 'name' and 'typed' will always contain only lowercase letters.
Goal: To determine if the typed string could be derived from the name with some characters possibly being long pressed.
Steps:
• Traverse both the 'name' and 'typed' strings simultaneously.
• If characters in 'typed' match the current character in 'name', continue.
• If a character in 'typed' matches the previous character, it can be considered a result of long pressing.
• If there is a mismatch and the previous character in 'typed' isn't the same as the current character, return False.
• At the end, check if all characters from 'name' have been matched correctly.
Goal: Ensure that both 'name' and 'typed' are valid strings consisting of lowercase English letters.
Steps:
• The length of 'name' and 'typed' will not exceed 1000 characters.
Assumptions:
• The input strings contain only lowercase English letters.
• The typed string is potentially longer than the name due to long presses.
Input: Input: name = "alison", typed = "aalliiison"
Explanation: The typed string has multiple instances of the 'l', 'i', and 's' characters, which could have been long pressed. Hence, the output is True.

Input: Input: name = "joan", typed = "jooaann"
Explanation: The second 'o' in the typed string doesn't match any subsequent character in the name, so the output is False.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus