Problem · String
Validate a Word Abbreviation
Learn this problemProblem statement
Given strings word and abbreviation, determine whether abbreviation is a valid abbreviation of word.
Read both strings from left to right:
- A letter in
abbreviationmust equal the current letter inword. - A maximal run of decimal digits represents the number of letters to skip in
word. - A number cannot have a leading zero.
- A skip cannot move past the end of
word.
The abbreviation is valid only when both inputs are consumed completely.
Function
validWordAbbreviation(word: String, abbreviation: String) → booleanExamples
Example 1
word = "internationalization"abbreviation = "i12iz4n"return = trueThe abbreviation keeps i, skips 12 letters, keeps i and z, skips 4 letters, and keeps the final n.
Example 2
word = "apple"abbreviation = "a2e"return = falseAfter matching a and skipping two letters, the abbreviation's e is compared with l, so the strings do not match.
Example 3
word = "substitution"abbreviation = "s010n"return = falseThe numeric token begins with 0, so the abbreviation is invalid even though its numeric value could otherwise be parsed.
Constraints
wordcontains letters.abbreviationcontains letters and decimal digits.- Letter matching is case-sensitive.
- Every numeric token fits in a signed
32-bit integer.