925-长按键入

前言

Weekly Contest 107的第一题长按键入:

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

提示:

  1. name.length <= 1000
  2. typed.length <= 1000
  3. nametyped 的字符都是小写字母。

解题思路

这道题我是先使用了一个压缩算法把字符串进行压缩后再对比,这个压缩算法就是参考游程编码。而且在LeetCode上也有对应的题目,如443. 压缩字符串。而我的专栏中也有相关算法的文章900-RLE 迭代器。这个算法处理后,会将字符串压缩到一个数组中,该数组的偶数位索引对应的值为字符,奇数位索引对应的值为该字符重复出现的次数
后续只需要判断对应位置的字符是否相等以及出现次数即可。

实现代码

    /**
     * 925. 长按键入
     * @param name
     * @param typed
     * @return
     */
    public boolean isLongPressedName(String name, String typed) {
        boolean result=true;
        if(typed.length()>=name.length()){//输入字符串的长度必须不小于原字符串
            List names=compress(name);
            List typeds=compress(typed);
            if(names.size()==typeds.size()){//压缩后的数组长度必须一致
                for(int i=0;i<=names.size()-2;i=i+2){
                    if(names.get(i).equals(typeds.get(i))){
                        int nameTimes=Integer.valueOf(names.get(i+1));
                        int typedTimes=Integer.valueOf(typeds.get(i+1));
                        if(typedTimes compress(String str){
        List result=new ArrayList<>();
        char currentChar = 0;
        int currentIndex=0;
        for(int i=0;i

你可能感兴趣的:(java,压缩,算法)