Skip to content

Commit 2868340

Browse files
Merge pull request #3041 from benmak11/408
Create 0408-valid-word-abbreviation.java
2 parents 41307ff + 45aec8c commit 2868340

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: java/0408-valid-word-abbreviation.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean validWordAbbreviation(String word, String abbr) {
3+
int i = 0, j = 0;
4+
while (i < word.length() && j < abbr.length()) {
5+
if (word.charAt(i) == abbr.charAt(j)) {
6+
i++;
7+
j++;
8+
continue;
9+
}
10+
if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9')
11+
return false;
12+
int num = 0;
13+
while (j < abbr.length() && Character.isDigit(abbr.charAt(j))) {
14+
num = num * 10 + abbr.charAt(j) - '0';
15+
j++;
16+
}
17+
i += num;
18+
}
19+
return i == word.length() && j == abbr.length();
20+
}
21+
}

0 commit comments

Comments
 (0)