-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlong-pressed-name_925.py
59 lines (45 loc) · 1.67 KB
/
long-pressed-name_925.py
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
51
52
53
54
55
56
57
58
59
# 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.
# ---------------------------------------Runtime 40 ms Beats 28.91% Memory 16.38 MB Beats 97.35%---------------------------------------
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
if typed[0] != name[0]:
return False
l_name = len(name)
l_typed = len(typed)
i = j = 0
prev_n = None
while i < l_name and j < l_typed:
n = name[i]
t = typed[j]
if n == t:
i += 1
j += 1
prev_n = n
elif n != t and t == prev_n:
j += 1
else:
i = 0
j += 1
prev_n = None
s = set(typed[j:])
if not i == l_name:
return False
return (
not typed[j:]
or typed[j:]
and len(s) == 1
and s.pop() == name[-1]
or False
)