Show problems as
Classic problem #392EasyTwo PointersStringDynamic Programming

Is Subsequence

Real-world version: Shorthand Fits the Label
Tracked in this browser only
Read Is Subsequence on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def code_fits_label(code: str, label: str) -> bool:
Java
public boolean codeFitsLabel(String code, String label)