diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index 4d3543f4..ef712252 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -8,7 +8,7 @@
欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
# 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: