Trains the technique from
LeetCode 408Valid Word AbbreviationThis 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.
Example 1
Dropping the first five letters of "internal" leaves "nal", which is exactly what the rest of the shorthand spells.
Example 2
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
The digit run "012" opens with a zero, so this shorthand is invalid.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def matches_shorthand(label: str, short: str) -> bool:public boolean matchesShorthand(String label, String short_)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.