优化0925.长按键入python版本

This commit is contained in:
JaneyLin
2022-06-18 16:58:58 -05:00
committed by GitHub
parent 0b9737d754
commit cbaa9df25b

View File

@ -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]: # 相同时向后匹配
i += 1
j+=1 j+=1
else: # 不相同 # special case when there are consecutive repeating letters
if j == 0: return False # 如果第一位不相同直接返回false if i+1<len(name) and name[i]==name[i+1]:
# 判断边界为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 i+=1
else:
j+=1 j+=1
else: return False i+=1
# 说明name没有匹配完 else:
if i < m: return False return False
# 说明type没有匹配完 return i == len(name) and j==len(typed)
while j < n:
if typed[j] == typed[j-1]: j += 1
else: return False
return True
``` ```
### Go ### Go