Check the Delivery Label Rhythm
Context
A warehouse prints one lowercase label character for each shipment loaded onto a delivery route. The dispatcher expects the labels to alternate between two different characters, such as a-b-a-b-a. Determine whether the full label sequence follows this rule. An empty sequence and a one-character sequence are considered valid because they do not violate the pattern.
Problem
Write a function that checks whether a shipment-label string follows a repeating two-position pattern.
The first two characters, when present, define the pattern. They must be different. Every later character must equal the character two positions before it. Thus, the sequence alternates between the first character and the second character, while adjacent characters remain different.
Return True if the sequence is valid and False otherwise. Treat an empty string and a one-character string as valid. Scan the string from left to right without constructing another sequence.
Examples
labels = "ababa"Truelabels = "abc"Falselabels = "aabb"FalseConstraints
0 <= len(labels) <= 100000labels: alphabet lowercaselabels contains only lowercase letters.- Types:
labelsis str; result is bool
Function signature
def validate_delivery_labels(labels: str) -> bool
labelsstr- The sequence of lowercase shipment-label characters in loading order.
- returns bool
- True when the labels form a valid period-two sequence with distinct neighboring characters; otherwise False.
Adapted from MBPP problem task_914 (CC BY 4.0). Rewritten, extended and verified by Iksha.