All problems
0219HardHash TableStringSliding Window

Code Tape Offsets

Tracked in this browser only
Write code

Trains the technique from

LeetCode 30Substring with Concatenation of All Words

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.

A sorting line prints one unbroken strip of lowercase letters, handed to you as tape. A run sheet lists the codes the line was told to stamp, handed to you as codes. Every entry of codes has the same length, and an entry may be listed more than once.

A block is a stretch of the tape that is exactly the entries of codes written one after another in some order, with nothing in between and nothing left over. The order is free, but the tally is not: an entry listed twice must be written twice inside the block, so every block is len(codes) codes long.

Return the position of every block start, counted from 0 and given in increasing order. Report each position once. Return an empty list when the tape holds no block. Block positions may overlap.

Examples

Example 1

Input
tape = "zzabcbcayy", codes = ["abc", "bca"]
Output
[2]

From position 2 the tape reads abcbca, which is abc then bca, so both run-sheet entries appear once each. No other position starts six letters that split into those two codes.

Example 2

Input
tape = "abababab", codes = ["ab", "ab"]
Output
[0, 2, 4]

The run sheet asks for ab twice, so a block is four letters long. Positions 0, 2 and 4 each read abab, which splits into ab and ab. Position 1 reads baba and position 3 reads baba, neither of which splits into two copies of ab.

Example 3

Input
tape = "ababcd", codes = ["ab", "cd"]
Output
[2]

Position 2 reads abcd, which is ab then cd. Position 0 reads abab, which repeats ab and never stamps cd, so the tally does not match.

Constraints

  • 1 <= tape.length <= 10^4
  • 1 <= codes.length <= 5000
  • 1 <= codes[i].length <= 30
  • every entry of codes has the same length
  • tape and codes[i] hold 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_tape_offsets(tape: str, codes: list[str]) -> list[int]:
Java
public List<Integer> codeTapeOffsets(String tape, String[] codes)
September 7
Apply