638. Was It Just a Long Press
Someone typed a name on a keyboard where a key sometimes repeats itself. Given the intended NAME and what was actually TYPED, decide whether the typed string could be the name with some keys held down too long.
A long press repeats an existing character; it never inserts a different character and never skips one.
Print Yes or No.
alex aaleex -> Yes a and e were held
saeed ssaaedd -> No the e was not repeated but a d appeared twice
leelee lleeelee -> Yes
laiden laiden -> Yes no long press is still allowed
## Two pointers over two strings
i = 0 (name), j = 0 (typed)
while j < m:
if i < n and name[i] == typed[j]: i += 1; j += 1 matched normally
else if j > 0 and typed[j] == typed[j-1]: j += 1 a long press
else: answer No
answer: Yes if i == n
The typed pointer advances every round. The name pointer advances only on a genuine match, so a repeat in the typed string is consumed without spending anything from the name.
## The check at the end is not optional
The loop can consume the whole typed string while the name still has characters left.
aa typed as a exits with j == m but i == 1, and the answer is No.
Forgetting i == n is the most common bug here, and it only shows when the typed string is
shorter than the name.
## Why comparing with the previous typed character is right
A long press repeats whatever key was last pressed. At position j, the last key pressed
produced typed[j-1], so a repeat must equal it. Comparing against name[i-1] instead looks
similar and is wrong once the name pointer has moved on past a run.
## The input that separates it
saeed typed as ssaaedd answers No. Walking it: s matches, the second s is a long
press, a matches, the second a is a long press, e matches, then the typed d is compared
against the name's second e and does not match, and it is not a repeat of the previous typed
character either. So it fails, correctly, even though every character of the name does appear in
the typed string.
That is why a plain subsequence check is not enough: this needs the RUN structure, not just the order.
## Cost
Time O(n + m). Space O(1).
Constraints - `1 ≤ length of each string ≤ 200000` - both strings contain only lowercase letters
Input
The first line contains the intended name. The second line contains what was typed.
Output
Print Yes or No on one line.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.