Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cbaa9df

Browse files
优化0925.长按键入python版本
1 parent 0b9737d commit cbaa9df

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

‎problems/0925.长按键入.md‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,21 @@ class Solution {
129129
```
130130
### Python
131131
```python
132-
class Solution:
133-
def isLongPressedName(self, name: str, typed: str) -> bool:
134-
i, j = 0, 0
135-
m, n = len(name) , len(typed)
136-
while i< m and j < n:
137-
if name[i] == typed[j]: # 相同时向后匹配
138-
i += 1
139-
j += 1
140-
else: # 不相同
141-
if j == 0: return False # 如果第一位不相同,直接返回false
142-
# 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"
143-
while j < n - 1 and typed[j] == typed[j-1]: j += 1
144-
if name[i] == typed[j]:
145-
i += 1
146-
j += 1
147-
else: return False
148-
# 说明name没有匹配完
149-
if i < m: return False
150-
# 说明type没有匹配完
151-
while j < n:
152-
if typed[j] == typed[j-1]: j += 1
153-
else: return False
154-
return True
132+
i = j = 0
133+
while(i<len(name) and j<len(typed)):
134+
# If the current letter matches, move as far as possible
135+
if typed[j]==name[i]:
136+
while j+1<len(typed) and typed[j]==typed[j+1]:
137+
j+=1
138+
# special case when there are consecutive repeating letters
139+
if i+1<len(name) and name[i]==name[i+1]:
140+
i+=1
141+
else:
142+
j+=1
143+
i+=1
144+
else:
145+
return False
146+
return i == len(name) and j==len(typed)
155147
```
156148

157149
### Go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /