# LeetCode: 925. 长按键入¶

## 1、题目描述¶

示例 1：



• name.length <= 1000
• typed.length <= 1000
• name 和 typed 的字符都是小写字母。

## 2、解题思路¶

• 设置两个下标，分别对应name和typed匹配到的位置
• 每次截取name相同的字母进行匹配，匹配多个typed相同字母
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
if (not name and typed) or (len(name) > len(typed)):
return False

name_pos = 0
type_pos = 0

while name_pos < len(name):
temp_pos = name_pos + 1
while temp_pos < len(name) and name[temp_pos] == name[name_pos]:
temp_pos += 1

if not typed[type_pos:].startswith(name[name_pos:temp_pos]):
return False
type_pos += temp_pos - name_pos

while type_pos < len(typed) and typed[type_pos] == name[name_pos]:
type_pos += 1

name_pos = temp_pos
if type_pos >= len(typed):
return True
else:
return False


class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:

if (not name and typed) or (len(name) > len(typed)):
return False

pos_name = 0
pos_typed = 0

while pos_name < len(name) and pos_typed < len(typed):
if name[pos_name] == typed[pos_typed]:
pos_name += 1
pos_typed += 1
else:
if typed[pos_typed] == name[pos_name - 1]:
pos_typed += 1
else:
return False

if pos_name == len(name):
if pos_typed == len(typed):
return True
else:
if len(set(typed[pos_typed:])) == 1 and typed[-1] == name[-1]:
return True
else:
return False
else:
return False