Valid Word Abbreviation
Real-world version: Checking a Shorthand Archive LabelThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def matches_shorthand(label: str, short: str) -> bool:public boolean matchesShorthand(String label, String short_)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 18 test cases: 18 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's