All problems
0266EasyTwo PointersStringDynamic Programming

Shorthand Fits the Label

Tracked in this browser only
Write code

Trains the technique from

LeetCode 392Is Subsequence

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 archivist writes a short code on a box and a longer label on the shelf card it belongs to. Both are made of lowercase English letters.

The code fits the label when every letter of the code can be matched to a position of the label so that the matched positions increase from the first letter of the code to the last, and no position of the label is used twice. Matched positions do not have to sit next to each other, and letters of the label may be left unmatched.

Return true when the code fits the label and false when it does not. A code with no letters fits every label.

Examples

Example 1

Input
code = "fig", label = "flight"
Output
true

Matching `f` to position 0, `i` to position 2 and `g` to position 3 uses increasing positions of the label for the three letters in order.

Example 2

Input
code = "gif", label = "flight"
Output
false

The label carries `g` only at position 3, and it holds no `i` or `f` after that position, so the letters cannot be matched in the order the code gives them.

Example 3

Input
code = "tt", label = "tap"
Output
false

The label holds one `t` and a position cannot be used twice, so the second `t` of the code has nothing left to match.

Constraints

  • 0 <= code.length <= 100
  • 0 <= label.length <= 10^4
  • code and label consist of lowercase English letters only

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 code_fits_label(code: str, label: str) -> bool:
Java
public boolean codeFitsLabel(String code, String label)
September 7
Apply