Long Pressed Name
Decide whether a typed string could be a name typed with some keys held down too long.
Your friend is typing your namenametypedtruetypednamefalse
Example 1
Output: true
Explanation: the a and the e were long pressed.
Example 2
Output: false
Explanation: name needs two e's in a row, but typed has only one.
The Idea
You have probably held a key a moment too long and seen "helllo" on the screen. Here your friend types "alex", and the screen shows "aaleex". Could that come from typing "alex" with a few keys held down?
Look at the picture below. The top row is nametypedtyped
Every letter of the name got a line, in order, and every other typed character is a long press. So the answer for Example 1 is true
Example 2 goes wrong partway. The name is "saeed" and typedfalse
So each typed character must be one of two things: the next letter of the name still to be typed, or a repeat of the character typed just before it. Anything else is a wrong key. How do we check that without guessing which characters are long presses?
Keep one marker in each string and move both from left to right. The marker in typedname
Solution Approach
Turning that idea into code means settling three things: what the two markers hold, which test to try first on each typed character, and how to treat the first character and the end of typed
The two markers
What should each marker hold? inamejtypedji
Matching before skipping
What if a typed character is both the next letter of the name and a repeat of the character before it? Take the name "leelee" typed as "lleeelee". After l, a long press of l and the first e, the next typed e could be the second e of the name or a long press of the first.
The right choice is the match. A letter of the name has to be typed at some point, while a long press is optional. A version that tests for a repeat first counts every e after the first as a long press. The name's second e is never matched, and that version returns false
The first character and the end of typed
Is there a character that cannot be a long press? The first character of typed
And when typedtrueiname
Java Solution
The method isLongPressedName
- First, set
to 0, the position of the next letter ofi still to be typed.name - Read
with atyped loop, movingfor from the first character to the last, and keep the current character inj .c - If
is still insidei andname equalsname.charAt(i) , it is a match: movec forward.i - Otherwise, if
is 0 orj differs from the previous typed character,c , it is a wrong key: returntyped.charAt(j - 1) . If neither holds,false is a long press, and the loop just moves on.c - After the loop, return whether
equalsi , that is, whether every letter of the name was typed.name.length()
The main
public class LongPressedName {
public static boolean isLongPressedName(String name, String typed) {
int i = 0; // next character of name to match
for (int j = 0; j < typed.length(); j++) {
char c = typed.charAt(j);
if (i < name.length() && name.charAt(i) == c) {
i++; // matches the next intended character
} else if (j == 0 || c != typed.charAt(j - 1)) {
return false; // not a match and not a repeat: a wrong key
}
// otherwise c repeats the previous typed character: a long press, skip it
}
return i == name.length(); // every character of name was typed
}
public static void main(String[] args) {
System.out.println(isLongPressedName("alex", "aaleex"));
System.out.println(isLongPressedName("saeed", "ssaaedd"));
System.out.println(isLongPressedName("leelee", "lleeelee"));
System.out.println(isLongPressedName("alex", "aaleexa"));
System.out.println(isLongPressedName("alex", "aale"));
System.out.println(isLongPressedName("a", "ba"));
System.out.println(isLongPressedName("alex", "alex"));
}
}
Running it prints:
true
false
true
false
false
false
true
The truefalsetrue
The fourth line is falseifalseitrue
The sixth line is "ba", rejected at its first character by the j == 0typed.charAt(j - 1)StringIndexOutOfBoundsExceptiontrue
Complexity
The loop reads typed
- Time: O(n), where n is the length of
. Each step compares at most two pairs of characters, andtyped moves forward at most once per step, soi adds no extra passes.name - Space: O(1). The method keeps two positions and one
, and never copies or splits the strings.char