CPP iterators solution


  • 0
    class Solution {
    public:
        bool validWordAbbreviation(string word, string abbr) {
            string::iterator iX = word.begin(), iY = abbr.begin();
            
            int num = 0;
            while (iX != word.end() && iY != abbr.end()) {
                if (*iX == *iY) {
                    iX++, iY++;
                    continue;
                }
                if (*iY <= '0' || *iY > '9') return false; // abbr start number with 0 or has character different with word.
                auto start = iY;
                while (iY != abbr.end() && isdigit(*iY)) iY++;
                int num = stoi(abbr.substr(start - abbr.begin(), iY - start));
                iX += num;
            }
            return iX == word.end() && iY == abbr.end();
        }
    };
    

Log in to reply
 

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