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