Junior
Valid Word Abbreviation
Given a non-empty string word and an abbreviation abbr, determine whether abbr is a valid abbreviation of word.
A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths. The lengths should not have leading zeros.
When validating abbr:
- A lowercase letter in
abbrmust match the corresponding character inword. - A number in
abbrmeans skip that many characters inword. - A number must not contain leading zeros.
Return true if abbr is a valid abbreviation of word; otherwise, return false.
Example 1
Input
word = "internationalization", abbr = "i12iz4n"Output
trueThe abbreviation keeps
i, skips 12 characters, keeps iz, skips 4 characters, and keeps n, matching the entire word.Example 2
Input
word = "apple", abbr = "a2e"Output
falseAfter matching
a and skipping 2 characters, the next expected character is l, not e, so the abbreviation is invalid.Constraints
- 1 <= word.length <= 20
- 1 <= abbr.length <= 10
- word consists of only lowercase English letters.
- abbr consists of lowercase English letters and digits.
- All integers in abbr will fit in a 32-bit signed integer.