• 925. 长按键入


    你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。

    你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。

    示例 1:

    输入:name = "alex", typed = "aaleex"
    输出:true
    解释:'alex' 中的 'a' 和 'e' 被长按。
    示例 2:

    输入:name = "saeed", typed = "ssaaedd"
    输出:false
    解释:'e' 一定需要被键入两次,但在 typed 的输出中不是这样。
    示例 3:

    输入:name = "leelee", typed = "lleeelee"
    输出:true
    示例 4:

    输入:name = "laiden", typed = "laiden"
    输出:true
    解释:长按名字中的字符并不是必要的。
     

    提示:

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

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/long-pressed-name
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class Solution:
        def isLongPressedName(self, name: str, typed: str) -> bool:
            # cnt1=collections.Counter(name)
            # cnt2=collections.Counter(typed)
            # for i in name:
            #     if cnt1[i]>cnt2[i]:return False
            if name[-1]!=typed[-1] or name[0]!=typed[0] or len(name)>len(typed):return False
            i=0
            j=0
            while i<len(name):
                
                a,b=name[i],typed[j]
                if a!=b:return False
                cnt1=cnt2=0
                
                while name[i]==a:
                    i+=1
                    cnt1+=1
                    if i>=len(name)-1:break
                while typed[j]==b:
                    j+=1
                    cnt2+=1
                    if j>=len(typed)-1:break
                if cnt1>cnt2:return False
            return True
  • 相关阅读:
    Qt中使用cout, cin, cerr
    linux下清理系统垃圾
    linux清理内存命令
    boost 特点
    linux boost 安装
    valgrind 的使用及错误信息分析
    ArcGIS Engine 编辑介绍
    ArcGIS Engine 编辑- IWorkspaceEdit
    ArcGIS Engine 编辑- ITask
    CreateFeature与CreateFeatureBuffer区别
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13851849.html
Copyright © 2020-2023  润新知