forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
25 lines (25 loc) · 727 Bytes
/
s1.cpp
File metadata and controls
25 lines (25 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// OJ: https://leetcode.com/problems/valid-word-abbreviation/
// Author: github.com/lzl124631x
// Time: O(M+N)
// Space: O(1)
class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
int i = 0, j = 0, M = word.size(), N = abbr.size();
while (i < M && j < N) {
if (isalpha(abbr[j])) {
if (word[i] != abbr[j]) return false;
++i;
++j;
continue;
}
int len = 0;
while (j < N && isdigit(abbr[j])) {
len = 10 * len + (abbr[j++] - '0');
if (!len) return false;
}
i += len;
}
return i == M && j == N;
}
};