925. Long Pressed Name
Easy
Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.
You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
Example 1:
Input: name = "alex", typed = "aaleex" Output: true Explanation: 'a' and 'e' in 'alex' were long pressed.
Example 2:
Input: name = "saeed", typed = "ssaaedd" Output: false Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
Example 3:
Input: name = "leelee", typed = "lleeelee" Output: true
Example 4:
Input: name = "laiden", typed = "laiden" Output: true Explanation: It's not necessary to long press any character.
Constraints:
1 <= name.length <= 10001 <= typed.length <= 1000- The characters of
nameandtypedare lowercase letters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution { public boolean isLongPressedName(String name, String typed) { int p1 = 0, p2 = 0, n1 = name.length(), n2 = typed.length(); while(p1<n1 && p2<n2){ if(name.charAt(p1)!=typed.charAt(p2)) return false; int q1 = p1+1, q2 = p2+1; while(q1<n1 && name.charAt(q1)==name.charAt(p1)) q1++; while(q2<n2 && typed.charAt(q2)==typed.charAt(p2)) q2++; int c1 = q1-p1; int c2 = q2-p2; if(c2<c1) return false; p1 = q1; p2 = q2; } return p1==n1 && p2==n2; } } |
No comments:
Post a Comment