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 abbr must match the corresponding character in word.
  • A number in abbr means skip that many characters in word.
  • A number must not contain leading zeros.

Return true if abbr is a valid abbreviation of word; otherwise, return false.

Example 1
Inputword = "internationalization", abbr = "i12iz4n"
Outputtrue
The abbreviation keeps i, skips 12 characters, keeps iz, skips 4 characters, and keeps n, matching the entire word.
Example 2
Inputword = "apple", abbr = "a2e"
Outputfalse
After 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.

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate