LeetCode 925: Long Pressed Name Solution
Master LeetCode problem 925 (Long Pressed Name), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
925. Long Pressed Name
Problem Explanation
Explanation
To solve this problem, we can iterate through both the name and typed strings simultaneously. We keep track of the current characters being compared in both strings. If the characters match, we move to the next characters in both strings. If they don't match, we check if the current character in typed is the same as the previous character in typed. If it is, this means the current character in typed might have been long pressed, so we move to the next character in typed and continue comparing. At the end, if we reach the end of both strings, it means the typed string can be formed by long-pressing the name string.
Time complexity: O(n), where n is the length of the longer of the two input strings.
Space complexity: O(1)
Solution Code
class Solution {
public boolean isLongPressedName(String name, String typed) {
int i = 0, j = 0;
while (j < typed.length()) {
if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
i++;
j++;
} else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) {
j++;
} else {
return false;
}
}
return i == name.length();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 925 (Long Pressed Name)?
This page provides optimized solutions for LeetCode problem 925 (Long Pressed Name) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 925 (Long Pressed Name)?
The time complexity for LeetCode 925 (Long Pressed Name) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 925 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 925 in Java, C++, or Python.