-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0925.Long_Pressed_Name.py
More file actions
51 lines (34 loc) Β· 1.42 KB
/
0925.Long_Pressed_Name.py
File metadata and controls
51 lines (34 loc) Β· 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
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 was not in the typed output.
Constraints:
1 <= name.length, typed.length <= 1000
name and typed consist of only lowercase English letters.
"""
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
i, j = 0, 0
while i < len(name) and j < len(typed):
if name[i] != typed[j]:
return False
name_cnt = 1
while i + 1 < len(name) and name[i + 1] == name[i]:
i += 1
name_cnt += 1
typed_cnt = 1
while j + 1 < len(typed) and typed[j + 1] == typed[j]:
j += 1
typed_cnt += 1
if name_cnt > typed_cnt:
return False
i += 1
j += 1
return i == len(name) and j == len(typed)