Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c01129c

Browse files
solves ##408: Valid word abbreviation in java
1 parent 4da0ba5 commit c01129c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@
313313
| 401 | [Binary Watch](https://leetcode.com/problems/binary-watch) | [![Java](assets/java.png)](src/BinaryWatch.java) [![Python](assets/python.png)](python/binary_watch.py) | |
314314
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves) | [![Java](assets/java.png)](src/SumOfLeftLeaves.java) [![Python](assets/python.png)](python/sum_of_left_leaves.py) | |
315315
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal) | [![Java](assets/java.png)](src/ConvertNumberToHexadecimal.java) [![Python](assets/python.png)](python/convert_a_number_to_hexadecimal.py) | |
316-
| 408 | 🔒 [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation) | | |
316+
| 408 | 🔒 [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation) | [![Java](assets/java.png)](src/ValidWordAbbreviation.java) | |
317317
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome) | [![Java](assets/java.png)](src/LongestPalindrome.java) [![Python](assets/python.png)](python/longest_palindrome.py) | |
318318
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz) | [![Java](assets/java.png)](src/FizzBuzz.java) [![Python](assets/python.png)](python/fizz_buzz.py) | |
319319
| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number) | [![Java](assets/java.png)](src/ThirdMaximumNumber.java) [![Python](assets/python.png)](python/fizz_buzz.py) | |

‎src/ValidWordAbbreviation.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://leetcode.com/problems/valid-word-abbreviation
2+
// T: O(|word|)
3+
// S: O(1)
4+
5+
public class ValidWordAbbreviation {
6+
public boolean validWordAbbreviation(String word, String abbr) {
7+
if(abbr.length() > word.length()) {
8+
return false;
9+
}
10+
11+
int i = 0, j = 0;
12+
while(i < word.length() && j < abbr.length()) {
13+
final char wc = word.charAt(i), ac = abbr.charAt(j);
14+
// if both characters are different, return false
15+
if(Character.isLetter(ac) && wc != ac) return false;
16+
else{
17+
// encountering a digit
18+
if(Character.isDigit(ac)){
19+
int a = ac - '0';
20+
if(a == 0) return false;
21+
// while we get digits, we create bigger number
22+
while(j + 1 < abbr.length() && Character.isDigit(abbr.charAt(j + 1))){
23+
a = a * 10 + abbr.charAt(j + 1) - '0';
24+
j++;
25+
}
26+
// jump i end of digits position
27+
i += a - 1;
28+
}
29+
}
30+
j++;
31+
i++;
32+
}
33+
return i == word.length() && j == abbr.length();
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /