Skip to content

Commit a1a08ad

Browse files
author
weiy
committed
long pressed name easy
1 parent 95068ff commit a1a08ad

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

String/LongPressedName.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
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.
3+
4+
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.
5+
6+
7+
8+
Example 1:
9+
10+
Input: name = "alex", typed = "aaleex"
11+
Output: true
12+
Explanation: 'a' and 'e' in 'alex' were long pressed.
13+
Example 2:
14+
15+
Input: name = "saeed", typed = "ssaaedd"
16+
Output: false
17+
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
18+
Example 3:
19+
20+
Input: name = "leelee", typed = "lleeelee"
21+
Output: true
22+
Example 4:
23+
24+
Input: name = "laiden", typed = "laiden"
25+
Output: true
26+
Explanation: It's not necessary to long press any character.
27+
28+
29+
Note:
30+
31+
name.length <= 1000
32+
typed.length <= 1000
33+
The characters of name and typed are lowercase letters.
34+
35+
判断 typed 里 是否存在 name,保证顺序的。
36+
37+
思路:
38+
typed 和 name 各一个指针,命中了就一起走,没命中就typed走。最后判断指针是否指向了name尾。
39+
40+
测试地址:
41+
https://leetcode.com/contest/weekly-contest-107/problems/long-pressed-name/
42+
43+
"""
44+
class Solution(object):
45+
def isLongPressedName(self, name, typed):
46+
"""
47+
:type name: str
48+
:type typed: str
49+
:rtype: bool
50+
"""
51+
x = 0
52+
y = 0
53+
54+
x_length = len(name)
55+
y_length = len(typed)
56+
57+
while x < x_length and y < y_length:
58+
59+
if typed[y] == name[x]:
60+
y += 1
61+
x += 1
62+
else:
63+
y += 1
64+
65+
if x == x_length:
66+
return True
67+
return False

0 commit comments

Comments
 (0)