1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: bool validWordAbbreviation(string word, string abbr) { word += " ", abbr += " "; int m = word.length(), n = abbr.length(), i = 0, j = 0; for (int s = 0; i < m && j < n; i += s, ++j) { if (isdigit(abbr[j])) { if (abbr[j] == '0') return false; int x = 0; while (isdigit(abbr[j])) { x = x * 10 + abbr[j] - '0'; ++j; } --j; s = x; } else { if (word[i] != abbr[j]) return false; s = 1; } } return i == m && j == n; } };
|