Is Subsequence
Real-world version: Shorthand Fits the LabelThe 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.
def code_fits_label(code: str, label: str) -> bool:public boolean codeFitsLabel(String code, String label)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 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
More on the same topics
- Longest Palindromic SubstringReal-world version: Longest Mirror RunMedium
- Palindromic SubstringsReal-world version: Reversible Tape StretchesMedium
- Find the Lexicographically Smallest Valid SequenceReal-world version: Cheapest Probe Slots On A ReelMedium
- Longest String ChainReal-world version: Longest Chain of Grown LabelsMedium