PracticeStrings

Validate a Repeating Shipment Label

Easy/MediumStringstraversalstate trackingcomparison Time not estimated Not started

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

Example 1
Input: code = "cold"label = "coldcold"
Output: True
Explanation: The call with label "coldcold" and code "cold" returns true. Its final traced match state is true after position 8, supporting the executed output.
Example 2
Input: code = "bl ue"label = "blueblue"
Output: False
Explanation: The call with label "blueblue" and code "bl ue" returns false. Its final traced match state is false, supporting the executed output.
Example 3
Input: code = "north"label = "northnorthn"
Output: False
Explanation: The call with label "northnorthn" and code "north" returns false. Its final traced match state is false, supporting the executed output.

Constraints

  • 0 <= len(label) <= 100000
  • 0 <= len(code) <= 100000
  • label and code contain only lowercase letters and spaces.
  • Types: label is str, code is str; result is bool

Function signature

def is_repeating_shipment_label(label: str, code: str) -> bool
label str
The complete shipment label.
code str
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.
Approved · c036-abs_274_v314 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1