刷题-Leetcode-925. 长按键入

925. 长按键入

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/long-pressed-name/submissions/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目分析

遍历两个字符串

  • 相等 同时向后移动
  • 不等 看typed的前一个和当前是否相等并且j不是第一位。相等,向后移动;不等,返回false;
class Solution {
    
    
public:
    bool isLongPressedName(string name, string typed) {
    
    
        int i = 0, j = 0;
        while(i <= name.size() && j <= typed.size()){
    
    
            if(name[i] == typed[j]){
    
    
                i++, j++;
            }else{
    
    
                if(j > 0 && typed[j-1] == typed[j]){
    
    
                    j++;
                }else{
    
    
                    return false;
                }
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_42771487/article/details/120966021