mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
添加0925.长按键入python3代码
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
# 925.长按键入
|
||||
|
||||
题目链接:https://leetcode-cn.com/problems/long-pressed-name/
|
||||
你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
|
||||
|
||||
你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。
|
||||
@ -100,7 +100,31 @@ public:
|
||||
Java:
|
||||
|
||||
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
|
||||
```
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
Reference in New Issue
Block a user