Valid Word Abbreviation

EasyTwo Pointerstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A word can be abbreviated by replacing non-empty substrings with their lengths written in decimal. For example, "substitution" can become "s10n", "sub4u4", or "12". An abbreviation is only valid if every number is written without leading zeros (so "s010n" and "0"-style numbers are invalid) and the numbers and letters account for exactly the characters of the word. Given word and abbr, return true if abbr is a valid abbreviation of word. A run of consecutive digits in abbr is always read as a single number.

Examples

Input: word = "internationalization", abbr = "i12iz4n"
Output: true
"i" + 12 skipped letters + "iz" + 4 skipped letters + "n" rebuilds the word.
Input: word = "apple", abbr = "a2e"
Output: false
After "a" and skipping "pp", the next letter is "l", not "e".
Input: word = "substitution", abbr = "s010n"
Output: false
The number "010" has a leading zero.

Constraints

  • 1 <= word.length <= 20
  • `word` consists of lowercase English letters
  • 1 <= abbr.length <= 10
  • `abbr` consists of lowercase English letters and digits

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems