cswork / libraryOpen practice workspace

Interview questions · Salesforce

Validate a Generalized Word Abbreviation

Validate whether a lowercase word matches a generalized abbreviation whose positive numbers skip characters. Enforce exact letter matching, full consumption, multi-digit counts, and the prohibition on leading zeroes.

Problem

Determine whether an abbreviation validly represents a word. An abbreviation contains lowercase letters and positive decimal numbers. A number means “skip this many characters” in the word. Letters must match the next unskipped word character exactly.

A numeric token may not start with 0. Both the word and abbreviation must be consumed completely.

Function Contract

Implement isValidWordAbbreviation(word, abbreviation) and return a Boolean.

Constraints & Assumptions

  • 1 <= len(word) <= 100,000.
  • 0 <= len(abbreviation) <= 100,000.
  • word contains lowercase English letters.
  • abbreviation contains lowercase English letters and digits.
  • A multi-digit number is parsed as one skip count.
  • Skip counts may be larger than the remaining word length; that makes the abbreviation invalid.

Clarifying Questions to Ask

  • Is zero a legal skip? No, so any numeric token beginning with 0 is invalid.
  • Are adjacent digits one number? Yes.
  • Must letter comparison be case-sensitive? Inputs are lowercase and matching is exact.
  • Is an empty abbreviation valid for a nonempty word? No.

Hint: Advance two pointers with two token types

On a letter, compare and advance both pointers by one. On a digit, parse the complete number and advance only the word pointer by that count.

Examples

  • word = "internationalization", abbreviation = "i18n" returns true.
  • word = "substitution", abbreviation = "s10n" returns false.
  • word = "apple", abbreviation = "a3e" returns true.
  • word = "apple", abbreviation = "a03e" returns false.
  • word = "word", abbreviation = "5" returns false.

Evaluation Focus

  • Rejects leading zeroes, oversize skips, mismatched letters, and partial consumption.
  • Parses long numeric tokens without integer overflow by stopping once the skip exceeds the remaining length.
  • Runs in O(len(word) + len(abbreviation)) time and O(1) auxiliary space.

Extensions to Discuss

  1. How would literal digits inside the word be escaped?
  2. Can the parser report the first failing abbreviation index?
  3. How would wildcard letters differ from numeric skips?

Community answers

Nothing here yet.

Join in on CSWork →