【力扣】:长按键入

你的朋友正在使用键盘输入他的名字 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
解释:长按名字中的字符并不是必要的。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/long-pressed-name

class Solution {
    
    
    public boolean isLongPressedName(String name, String typed) {
    
    
        int len=name.length();
        int lent=typed.length();
        char[] n_arr=name.toCharArray();
        char[] t_arr=typed.toCharArray();
        int i=0;
        int j=0;
        while(j<lent){
    
    
            if(i<len&&n_arr[i]==t_arr[j]){
    
    //如果相等,判断下一位
                i++;
                j++;
            }
            else if(i-1>=0&&n_arr[j]==t_arr[i-1]){
    
    //如果在typed 中连续出现同一字母,
                                                  // 且该字母和n_arr[i-1]相同,
                                                 // 则只j++
                j++;
            }
            else
                return false;
        }
        return i==len;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_44292334/article/details/112975776