408. 有效单词缩写

题目
类型:字符串
难度:简单
题目:判断一个字符串是否是另一个字符串的有效缩写。注意缩写中出现0就是错的。

class Solution {
public:
    bool validWordAbbreviation(string word, string abbr) {
        int t = 0, i, j;
        bool ok = true;
        for(i = 0, j = 0; i < abbr.size(); i++){
            if(isdigit(abbr[i])){
                t = t*10+abbr[i]-'0';
                if(t==0) ok = false;
            }else{
                j+=t;
                t = 0;
                if(j >= word.size() || word[j] != abbr[i]) ok = false;
                j++;
            }
        }
        return j+t==word.size() && ok;
    }
};
发布了1205 篇原创文章 · 获赞 54 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/SYaoJun/article/details/105135170
408
今日推荐