刷题记录: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;
    }
};
相关推荐
lengjingzju24 分钟前
基于IMake的 GCC 编译与链接选项深度解析:构建高效、安全、可调试的现代软件
c++·安全·性能优化·软件构建·开源软件
xu_yule42 分钟前
算法基础(数论)—算法基本定理
c++·算法·算数基本定理
云栖梦泽1 小时前
鸿蒙应用AI赋能与国际化落地实战:让待办应用跨越语言与智能边界
开发语言·鸿蒙系统
CoderCodingNo1 小时前
【GESP】C++五级真题(结构体排序考点) luogu-B3968 [GESP202403 五级] 成绩排序
开发语言·c++·算法
想做后端的小C1 小时前
Java:接口回调
java·开发语言·接口回调
YGGP2 小时前
【Golang】LeetCode 32. 最长有效括号
算法·leetcode
麒qiqi2 小时前
理解 Linux IO 多路复用
开发语言·数据库
MediaTea3 小时前
Python:模块 __dict__ 详解
开发语言·前端·数据库·python
自然常数e3 小时前
字符函数和字符串函数
c语言·算法·visual studio