刷题记录:LeetCode 925.长按键入

题目:

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

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

复制代码
class Solution {
public:
    bool isLongPressedName(string name, string typed) {
        int nn = 0;
        int tt = 0;
        while (nn < name.size() || tt < typed.size())
        {
            if (name[nn] == typed[tt])
            {
                nn++;
                tt++;
            }
            else
            {
                if (tt == 0) return false;
                else 
                {
                    while (tt < typed.size() && typed[tt - 1] == typed[tt]) tt++;
                    if (name[nn] != typed[tt]) return false;
                    else 
                    {
                        tt++;
                        nn++;
                    }
                }
            }
        }
        if (tt < typed.size()) return false;
        return true;
    }
};
相关推荐
kiracrimson13 小时前
栈与队列容器:适配器、deque 与 priority_queue
开发语言·c++·适配器模式
北冥you鱼14 小时前
Go 语言 CRUD 实战:从接口设计到真实业务场景
开发语言·后端·golang
Shaoshing14 小时前
LeetCode100 42.接雨水
java·算法·leetcode
郭小李14 小时前
springmvc 拦截器
java·开发语言·前端
web打印社区14 小时前
Chrome-kiosk-printing能上生产吗
开发语言·前端·javascript·chrome·websocket
域限奇点访客14 小时前
用AI生成问卷的网站怎么选?看题目录入结构、题型映射和生成后的校验
算法
大风起兮云飞扬丶14 小时前
Java——日期类
java·开发语言
Wang's Blog14 小时前
Java 项目实战:交友项目 - 联系人管理需求分析
java·开发语言·交友
lusklusklusk15 小时前
Python 全局变量与局部变量区别——global 关键字、__main__ 块与作用域示例
开发语言·前端·python
马剑威(威哥爱编程)15 小时前
Python 智能体开发实战 01:装了三遍环境还在报错?虚拟环境和 DeepSeek 一次配到位
开发语言·人工智能·python·agent