Number of Strings That Appear as Substrings in Word
Real-world version: Codes Printed On The Label StripThe statement here is our own: the same inputs, outputs and technique.
A label printer has produced one long strip of lowercase letters, given as strip. A picking sheet lists short product codes in codes, each also made of lowercase letters.
A code is printed on the strip when its letters occur in the strip next to each other and in the same order, that is, when the code is exactly some run of consecutive letters of the strip.
Return how many entries of codes are printed on the strip. The sheet may list the same code more than once, and each entry counts on its own.
Examples
Example 1
- Input
- codes = ["ab", "ab", "zz"], strip = "cabd"
- Output
- 2
The letters `ab` sit next to each other in the strip at offset 1, and the sheet lists that code twice, so it contributes two. The strip holds no `z` at all.
Example 2
- Input
- codes = ["kite", "ki", "ite", "kit", "tie"], strip = "spikite"
- Output
- 4
`kite`, `ki`, `ite` and `kit` each appear as a run of consecutive letters of the strip. `tie` does not: the strip has a `t`, an `i` and an `e`, but never in that order side by side.
Example 3
- Input
- codes = ["ac"], strip = "abc"
- Output
- 0
The strip holds both an `a` and a `c`, but a `b` stands between them, so the code is not a run of consecutive letters of the strip.
Constraints
- 1 <= codes.length <= 100
- 1 <= codes[i].length <= 100
- 1 <= strip.length <= 100
- codes[i] and strip hold 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 codes_on_strip(codes: list[str], strip: str) -> int:public int codesOnStrip(String[] codes, String strip)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 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 Common PrefixReal-world version: Shared Crate CodeEasy
- Shortest Word DistanceReal-world version: Closest Station Calls in the Announcement LogEasy
- Verifying an Alien DictionaryReal-world version: Catalogue Order Under a Custom Glyph SequenceEasy
- Find Common CharactersReal-world version: Tiles Every Rack Can SupplyEasy