Problem · String

Validate a Word Abbreviation

Learn this problem
EasySalesforce logoSalesforceFULLTIMEONSITE INTERVIEW
See Salesforce hiring insights

Problem 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 abbreviation must equal the current letter in word.
  • 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) → boolean

Examples

Example 1

word = "internationalization"abbreviation = "i12iz4n"return = true

The 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 = false

After 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 = false

The numeric token begins with 0, so the abbreviation is invalid even though its numeric value could otherwise be parsed.

Constraints

  • word contains letters.
  • abbreviation contains letters and decimal digits.
  • Letter matching is case-sensitive.
  • Every numeric token fits in a signed 32-bit integer.

More Salesforce problems

drafts saved locally
public boolean validWordAbbreviation(String word, String abbreviation) {
    // Write your code here.
}
word"internationalization"
abbreviation"i12iz4n"
expectedtrue
checking account