-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidWordAbbreviation.java
33 lines (33 loc) · 1.09 KB
/
ValidWordAbbreviation.java
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
//https://leetcode.com/problems/valid-word-abbreviation/
//s10n" is a valid abbreviation of "substitution" b
//Time O(m + n), where m is the length of word and n is the length of abbr.
// Space " O(1), which represents constant space.
class Solution {
public boolean validWordAbbreviation(String word, String abbr) {
int i=0, j=0;
while(i<word.length() && j<abbr.length()){
char x = word.charAt(i);
char y = abbr.charAt(j);
if(x==y){
i++;
j++;
} else if(y>='a' && y<='z')
return false;
else {
int num = 0;
if(y=='0')
return false;
while(j<abbr.length()&&abbr.charAt(j)>='0' && abbr.charAt(j)<='9'){
num = num*10+(abbr.charAt(j)-'0');
j++;
}
if(i+num>word.length())
return false;
i=i+num;
}
}
if(i==word.length() && j==abbr.length())
return true;
return false;
}
}