forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC408_ValidWordAbbreviation.java
More file actions
36 lines (34 loc) · 1.05 KB
/
LC408_ValidWordAbbreviation.java
File metadata and controls
36 lines (34 loc) · 1.05 KB
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
26
27
28
29
30
31
32
33
34
35
import java.util.*;
import java.io.*;
public class LC408_ValidWordAbbreviation {
public boolean validWordAbbreviation(String word, String abbr) {
if(word == null || abbr == null) return false;
int wLen = word.length(), aLen = abbr.length();
char ch = ' ';
int cnt = 0;
int index = -1;
for( int i = 0; i < aLen; ++i){
ch = abbr.charAt(i);
if('a' <= ch && ch <= 'z' || ch == '0'){ // ch == '0' removing the cases in which there is leading zeros
++index;
if(index >= wLen || ch != word.charAt(index)) return false;
}else{
cnt = 0;
//if(i < aLen - 1) ch = abbr.charAt(++i);
while('0' <= ch && ch <= '9' && i < aLen){
cnt = cnt * 10 + ch - '0';
if(i == aLen - 1){
++i;
break;
}
ch = abbr.charAt(++i);
}
index += cnt + 1;
if(index == wLen && i == aLen) return true;
if(index >= wLen || ch != word.charAt(index)) return false;
}
}
if(index != wLen - 1) return false;
return true;
}
}