1. 程式人生 > >leetcode925 Long Pressed Name

leetcode925 Long Pressed Name

給定字串 A 和 B ,輸入 A 時某些字母會手抖打多遍,問 B 是否可能是 A 手抖後的結果。

思路:暴力即可,兩個指標,滿足不了條件就return false

class Solution {
public:
    bool isLongPressedName(string name, string typed) {
        int i=0,j=0;
        int len1=name.length(),len2=typed.length();
        if(len1>len2) return false;
        while(i<len1||j<len2)
        { 
            if(name[i]==typed[j]){
                i++,j++;
            }
            else if(name[i-1]==typed[j])
            {
                j++;
            }
            else 
            {
                return false;
            }
        }
        return true;
    }
};