All problems
0537EasyTwo PointersString

Checking a Shorthand Archive Label

Tracked in this browser only
Write code

Trains the technique from

LeetCode 408Valid Word Abbreviation

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

An archive box is titled with a lowercase word label. To save ink a clerk writes a shorthand short, made of lowercase letters and digits, and it is read left to right like this: a letter stands for itself and has to line up with that same letter of the label, while a run of digits gives how many letters of the label were left out at that point.

Two rules keep the reading unambiguous. A run of digits is read as one whole number, so "12" asks for twelve letters to be dropped rather than one and then two. And a run of digits never starts with 0, which makes a shorthand such as "012" invalid whatever the label is.

Return true when reading short this way spells out label exactly, using up every letter of the label and every character of the shorthand, and false otherwise.

Examples

Example 1

Input
label = "internal", short = "5nal"
Output
true

Dropping the first five letters of "internal" leaves "nal", which is exactly what the rest of the shorthand spells.

Example 2

Input
label = "banana", short = "3an"
Output
false

Dropping three letters lands on the "a" at index 3, and the shorthand then spells "an", which covers only indices 3 and 4. Index 5 of the label is left over, so the shorthand does not spell the whole label.

Example 3

Input
label = "gadget", short = "012"
Output
false

The digit run "012" opens with a zero, so this shorthand is invalid.

Constraints

  • 1 <= label.length <= 20
  • label holds only lowercase English letters.
  • 1 <= short.length <= 10
  • short holds only lowercase English letters and digits.
  • Every number written in short fits in a 32-bit signed integer.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def matches_shorthand(label: str, short: str) -> bool:
Java
public boolean matchesShorthand(String label, String short_)
September 7
Apply