菜鸟leetcode 925题解析

it2025-02-27  22

本题可运用双指针进行解析 当j比typed长度小时,可以进行检查 先遍历第一位看是否相等,如果相等可检查下一位 如果下一位中两个指针不相等,若j和j的前一位重复,则可进行下一位的判断 但是要是name中的字符j中没有,则输出False 最后可进行一个判断,若i和原name的字符长度相等则返回True

class Solution: def isLongPressedName(self, name: str, typed: str) -> bool: i, j = 0, 0 while j < len(typed): if i < len(name) and name[i] == typed[j]: i += 1 j += 1 elif j > 0 and typed[j] == typed[j-1]: j += 1 else: return False if i == len(name): return True else: return False
最新回复(0)