mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
优化0925.长按键入python版本
This commit is contained in:
@ -129,29 +129,21 @@ class Solution {
|
|||||||
```
|
```
|
||||||
### Python
|
### Python
|
||||||
```python
|
```python
|
||||||
class Solution:
|
i = j = 0
|
||||||
def isLongPressedName(self, name: str, typed: str) -> bool:
|
while(i<len(name) and j<len(typed)):
|
||||||
i, j = 0, 0
|
# If the current letter matches, move as far as possible
|
||||||
m, n = len(name) , len(typed)
|
if typed[j]==name[i]:
|
||||||
while i< m and j < n:
|
while j+1<len(typed) and typed[j]==typed[j+1]:
|
||||||
if name[i] == typed[j]: # 相同时向后匹配
|
j+=1
|
||||||
i += 1
|
# special case when there are consecutive repeating letters
|
||||||
j += 1
|
if i+1<len(name) and name[i]==name[i+1]:
|
||||||
else: # 不相同
|
i+=1
|
||||||
if j == 0: return False # 如果第一位不相同,直接返回false
|
else:
|
||||||
# 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"
|
j+=1
|
||||||
while j < n - 1 and typed[j] == typed[j-1]: j += 1
|
i+=1
|
||||||
if name[i] == typed[j]:
|
else:
|
||||||
i += 1
|
return False
|
||||||
j += 1
|
return i == len(name) and j==len(typed)
|
||||||
else: return False
|
|
||||||
# 说明name没有匹配完
|
|
||||||
if i < m: return False
|
|
||||||
# 说明type没有匹配完
|
|
||||||
while j < n:
|
|
||||||
if typed[j] == typed[j-1]: j += 1
|
|
||||||
else: return False
|
|
||||||
return True
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
Reference in New Issue
Block a user