A smart-lock keypad occasionally has a contact stick: when a key sticks, a single press of it gets logged as two or more consecutive identical keystrokes instead of one. The keypad's log is otherwise perfectly faithful -- it never skips a keystroke the user made, never logs the wrong key for a press, and never reorders keystrokes.
Given the code the user intended to key in and the log the keypad actually recorded, determine whether the log is a plausible outcome of typing that exact code with zero or more keys sticking somewhere along the way.
Two lines:
name, a string of lowercase English letters.typed, a string of lowercase English letters.A single line containing true if typed could have been produced by long-pressing zero or more keys while entering name, and false otherwise.
Example 1
Input
alex aaleex
Expected
true
Explanation
Grouping typed by consecutive runs gives aa-l-ee-x, which lines up with name's a-l-e-x with each name letter matched by one or more repeats of the same letter in order, so the log is a plausible sticky-key recording. Output: true.
Example 2
Input
alex aaleexa
Expected
false
Explanation
Matching proceeds a-a(stick)-l-e-e(stick)-x successfully through all of name, but typed still has a trailing 'a' left over that is a different letter than the last matched key 'x', so it cannot be explained as either the next name character or a stuck repeat. Output: false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →