PracticeStrings

Check the Delivery Label Rhythm

EasyStringstraversalstate trackingcomparison Time not estimated Not started

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

Example 1
Input: labels = "ababa"
Output: True
Explanation: For "ababa", the executed result is true. The final traced validity value is true.
Example 2
Input: labels = "abc"
Output: False
Explanation: For "abc", the executed result is false. The final traced validity value is false at position 2.
Example 3
Input: labels = "aabb"
Output: False
Explanation: For "aabb", the first two characters are equal, so the required first-pair condition rejects the string.

Constraints

  • 0 <= len(labels) <= 100000
  • labels: alphabet lowercase
  • labels contains only lowercase letters.
  • Types: labels is str; result is bool

Function signature

def validate_delivery_labels(labels: str) -> bool
labels str
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.

Approved · c034-abs_314_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1