算法题笔记 - 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
解释:长按名字中的字符并不是必要的。

 

//4 ms

class Solution {
public:
    vector<vector<int> > parseCharFrequence(const string& str){
        vector<vector<int> > result;
        int idx = 0;
        vector<int> temp(2);
        temp[0] = str[0];
        temp[1] = 1;
        result.push_back(temp);
        for(int i=1; i<str.length(); i++)
        {
            if(str[i] == temp[0])
            {
                temp[1] = temp[1] + 1;
                result[idx] = temp;
            }
            else
            {
                temp[0] = str[i];
                temp[1] = 1;
                idx++;
                result.push_back(temp);
            }
        }
        result[idx] = temp;
        return result;
    }
    bool isLongPressedName(string name, string typed) {
        if(name.length() == 0)
        {
            return false;
        }
        if(typed.length() == 0)
        {
            return false;
        }
        vector<vector<int> > vecName = parseCharFrequence(name);
        vector<vector<int> > vecTyped = parseCharFrequence(typed);
        if(vecName.size() != vecTyped.size()){
            return false;
        }
        for(int i=1; i<vecName.size(); i++)
        {
            if((vecName[i][0] != vecTyped[i][0])||(vecName[i][1] > vecTyped[i][1]))
            {
                return false;
            }
        }
        return true;
    }
};

 

提示:

  1. name.length <= 1000
  2. typed.length <= 1000
  3. name 和 typed 的字符都是小写字母。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值