Leetcode 925. Long Pressed Name
Problem Explanation
This problem involves verifying if a string can be formed from another string through long pressing of keys.
Long pressing, in this case, implies that a character would be typed one or more times than what is intended. Hence, we are given a name (first string) and a supposed long pressed version of the name (second string), then we must determine if the second string can be formed from the first through long pressing.
To solve this, we need to iterate over the second string and compare each character with the corresponding character in the first string. If they are equal, then increase a counter which keeps track of the position in the first string. However, if they are not equal and either it's the first character of the second string or the character doesn't match the previous character in the second string, then return false. After the iteration, check if the counter equals the length of the first string and return the result.
Let's break it down with an example:
Example
"alex", "aaleex"
- 'a' in both strings match so the counter becomes 1
- 'a' does not equal 'l' in the first string but it equals the previous character in the second string so continue
- 'l' equals 'l' so the counter becomes 2
- 'e' equals 'e' so the counter becomes 3
- 'e' does not equal 'x' in the first string but it equals the previous character in the second string so continue
- 'x' equals 'x' so the counter becomes 4
- Check if the counter (4) equals length of first string (4). Since they are equal, return true
Python Solution
1 2python 3class Solution: 4 def isLongPressedName(self, name: str, typed: str) -> bool: 5 i = 0 6 for j in range(len(typed)): 7 #Increase the counter if characters match 8 if i < len(name) and name[i] == typed[j]: 9 i += 1 10 #Return false if characters are not same and previous character doesn't match 11 elif j == 0 or typed[j] != typed[j - 1]: 12 return False 13 #Check if counter equals length of input string 14 return i == len(name)
Java Solution
1
2java
3class Solution {
4 public boolean isLongPressedName(String name, String typed) {
5 int i = 0;
6 for (int j = 0; j < typed.length(); ++j)
7 if (i < name.length() && name.charAt(i) == typed.charAt(j))
8 i++;
9 else if (j == 0 || typed.charAt(j) != typed.charAt(j - 1))
10 return false;
11 return i == name.length();
12 }
13}
JavaScript Solution
1 2javascript 3class Solution { 4 isLongPressedName(name, typed) { 5 let i = 0; 6 for (let j = 0; j < typed.length; j++){ 7 if (i < name.length && name[i] === typed[j]) 8 i++; 9 else if (j === 0 || typed[j] !== typed[j - 1]) 10 return false; 11 } 12 return i === name.length; 13 } 14}
C++ Solution
1 2cpp 3class Solution { 4 public: 5 bool isLongPressedName(std::string name, std::string typed) { 6 int i = 0; 7 8 for (int j = 0; j < typed.length(); ++j) 9 if (i < name.length() && name[i] == typed[j]) 10 ++i; 11 else if (j == 0 || typed[j] != typed[j - 1]) 12 return false; 13 14 return i == name.length(); 15 } 16};
C# Solution
1 2csharp 3public class Solution { 4 public bool IsLongPressedName(string name, string typed) { 5 int i = 0; 6 7 for (int j = 0; j < typed.Length; ++j) 8 if (i < name.Length && name[i] == typed[j]) 9 i++; 10 else if (j == 0 || typed[j] != typed[j - 1]) 11 return false; 12 13 return i == name.Length; 14 } 15}
Ruby Solution
1 2ruby 3class Solution 4 def is_long_pressed_name(name, typed) 5 i = 0 6 count = 0 7 while count < typed.length 8 if i < name.length && name[i] == typed[count] 9 i += 1 10 elsif count == 0 || typed[count] != typed[count - 1] 11 return false 12 end 13 count += 1 14 end 15 return i == name.length 16 end 17end
The logic of this problem solution in Ruby language is quite similar to other solutions. We iterate over the typed
string with a while
loop, keep track of the position in the name
string with i
, and increase it every time the current characters of name
and typed
match. If we ever come across a character in typed
which is not equal to the current character in name
and also not equal to the previous character in typed
, we know that it is impossible to form this typed
string by long pressing keys while typing name
, so we return false
. If we come to the end of the while
loop without returning false
, we will return true if i
equals to the length of name
.
This approach works for all test cases because we ensure that each character in the typed
string is either the same as the present character in the name
string or the same as the previous character in the typed
string. This will guarantee that the typed
string was achieved by long-pressing the keys corresponding to the name
string.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.