c++ solution


  • 0
    B
    class Solution {
    public:
        bool validWordAbbreviation(string word, string abbr) {
            int i=0, j=0, num=0;
            
            while(i<word.length() && j<abbr.size()){
                if(isdigit(abbr[j])){
                    if(abbr[j]=='0' && num==0) return false;
                    num*=10;
                    num+=abbr[j++]-'0';
                }
                else{
                    i+=num;
                    num=0;
                    if(i>=word.size() || word[i++]!=abbr[j++]) return false;
                }
            }
            i+=num;
            
            return (i==word.size() && j==abbr.size());
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.