题目描述
你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。
输入:name = “alex”, typed = “aaleex”
输出:true
解释:‘alex’ 中的 ‘a’ 和 ‘e’ 被长按。
算法思路
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
if len(name)>len(typed):return False
j=0
tp=''
for i in name:
if j>=len(typed):return False
if typed[j]==i:
j+=1
tp=i
elif typed[j]==tp:
while typed[j]==tp:
j+=1
if j>=len(typed):return False
if typed[j]!=i:return False
j+=1
tp=i
else:
return False
return True
执行用时 :32 ms, 在所有 Python3 提交中击败了88.09%的用户
内存消耗 :13.7 MB, 在所有 Python3 提交中击败了13.04%的用户