LeetCode 925 Long key press to enter the LeetCode road of HERODING

Your friend is using the keyboard to enter his name. Occasionally, when typing the character c, the key may be long-pressed, and the character may be entered one or more times.

You will check the typed characters entered on the keyboard. If it corresponds to your friend's name (some characters may be long-pressed), then it returns True.

Example 1:

Input: name = "alex", typed = "aaleex"
Output: true
Explanation: The'a' and'e' in'alex ' are long pressed.

Example 2:

Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation:'e' must be typed twice, but this is not the case in typed output.

Example 3:

Input: name = "leelee", typed = "lleeelee"
Output: true

Example 4:

Input: name = "laiden", typed = "laiden"
Output: true
Explanation: Long-pressing the characters in the name is not necessary.

prompt:

name.length <= 1000
typed.length <= 1000
name 和 typed 的字符都是小写字母。

Problem-solving idea: This
problem is said to be a simple one, but it is actually very complicated and there are quite a few judgment conditions. Of course, it is logical to determine a good idea.

  • First of all, the analysis shows that when inputting the keys, the first key must be the same, and the difference is definitely wrong.
  • Secondly, when the corresponding typed is found to be different when traversing the name, it will traverse the same characters of the typed until they are the same, and return false if they are not the same.
  • If the typed is traversed and the name is not traversed, it means there must be an error, and false is returned.
  • Finally, if name is traversed and typed is not traversed, then the last element of typed is traversed, and false is returned if there are different characters.
    code show as below:
class Solution {
    
    
public:
    bool isLongPressedName(string name, string typed) {
    
    
        bool flag = true;
        int index = 0;
        if(name[0] != typed[0]){
    
    
            return false;
        }
        for(int i = 0; i < name.length(); i ++){
    
    
            if(index < typed.length()){
    
    
                if(name[i] == typed[index]){
    
    
                    index ++;
                    continue;
                }
                while(typed[index + 1] == typed[index] && (index + 1) < typed.length()){
    
    
                    index ++;
                }
                index ++;
                if(typed[index] != name[i]){
    
    
                    return false;
                }
                index ++;
            }
            else{
    
    
                return false;
            }
            
        }
        if(index < typed.length()){
    
    
            while(typed[index + 1] == typed[index] && index + 1 < typed.length()){
    
    
                index ++;
            }
            index ++;
        }
        if(index == typed.length()){
    
    
            return flag;
        }
        return false;
    }
};


/*作者:heroding
链接:https://leetcode-cn.com/problems/long-pressed-name/solution/zhi-xing-yong-shi-ji-bai-100si-lu-zui-qing-xi-de-d/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/

Guess you like

Origin blog.csdn.net/HERODING23/article/details/109193840