Validate a Repeating Shipment Label
Context
A warehouse prints shipment labels by repeating a short warehouse code. Given the complete label and a proposed code, decide whether the label contains one or more exact consecutive copies of that code. The check must compare characters literally, including spaces.
Problem
A shipment label is valid for a proposed warehouse code when the code is non-empty, the label contains at least one copy of it, and every character in the label matches the character at the same position within the repeating code. First ensure the label length is divisible by the code length. Then scan the label once, comparing position i with the code position i modulo the code length. Return true only when every comparison matches. If either the label is empty or the code is empty, return false.
Examples
code = "cold"label = "coldcold"Truecode = "bl ue"label = "blueblue"Falsecode = "north"label = "northnorthn"FalseConstraints
0 <= len(label) <= 1000000 <= len(code) <= 100000label and code contain only lowercase letters and spaces.- Types:
labelis str,codeis str; result is bool
Function signature
def is_repeating_shipment_label(label: str, code: str) -> bool
labelstr- The complete shipment label.
codestr- The proposed warehouse code to repeat.
- returns bool
- True if the non-empty code forms the entire label through one or more exact repetitions; otherwise false.
Adapted from MBPP problem task_874 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Treat spaces as ordinary characters.
- Use constant auxiliary space and a linear scan of the label.