c++ impl


  • 0
    T
    class Solution {
    public:
        bool validWordAbbreviation(string word, string abbr) {
            int i = 0, j = 0;
            for (int sw = word.size(), sa = abbr.size(); i < sw && j < sa; ) {
                if (abbr[j] >= '0' && abbr[j] <= '9') {
                    int n = abbr[j++]-'0';
                    if (!n) return false; // starting  with '0' isn't valid
                    while (j < sa && abbr[j] >= '0' && abbr[j] <= '9') {
                        n = n * 10 + abbr[j++] - '0';
                    }
                    i += n;
                }
                else if (word[i++] != abbr[j++]) 
                    return false;
            }
            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.