长按键入

🎯 问题描述(来源于LeetCode)

描述
你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被_长按_,而字符可能被输入 1 次或多次。
你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True
说明

  • 1 <= name.length, typed.length <= 1000

  • name 和 typed 的字符都是小写字母
    示例

  • 示例 1:

1
2
3
输入:name = "alex", typed = "aaleex"
输出:true
解释:'alex' 中的 'a' 和 'e' 被长按。
  • 示例 2:
1
2
3
输入:name = "saeed", typed = "ssaaedd"
输出:false
解释:'e' 一定需要被键入两次,但在 typed 的输出中不是这样。

💻 解题思路

思路1:分离双指针

思路1:代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
name_l=list(name)
typed_l=list(typed)
left_1,left_2=0,0
while left_2<len(typed_l) :
if left_1 <len(name_l) and name_l[left_1]==typed_l[left_2]:
left_1+=1
left_2+=1
elif left_2>0 and typed_l[left_2]==typed_l[left_2-1]:
left_2+=1
else:
return False
return left_1==len(name_l)

思路1:📊 性能分析

提交结果
  • 运行时间:0ms击败100.00%
  • 内存消耗:18.98MB击败58.31%
复杂度验证
  • 时间复杂度:O(N)O(N)
  • 空间复杂度:O(N)O(N)

思考