PracticeStrings

Last Label Checkpoint

BeginnerStringstraversalstate trackingcomparison Time not estimated Not started

Context

A warehouse system stores the lowercase letters printed along a shipment’s handling sequence. A supervisor asks where a particular label marker was seen most recently. Your function must report the zero-based position of the final occurrence, or indicate that the marker never appeared.

Problem

Implement a function that searches the shipment label sequence from left to right for the requested one-character marker. Return the zero-based index of its final occurrence. If the marker does not occur, return -1. The sequence may be empty, and repeated markers must be resolved in favor of the greatest index.

Examples

Example 1
Input: labels = "abacaba"marker = "a"
Output: 6
Explanation: For labels "abacaba" and marker "a", the executed result is 6. The final local best_pos is 6 and scan_idx is 7.
Example 2
Input: labels = "shipment"marker = "z"
Output: -1
Explanation: For labels "shipment" and marker "z", the executed result is -1. The final local best_pos is -1.
Example 3
Input: labels = ""marker = "m"
Output: -1
Explanation: For the empty labels string and marker "m", the executed result is -1. The final local best_pos is -1 and scan_idx is 0.

Constraints

  • 0 <= len(labels) <= 1000
  • labels: alphabet lowercase
  • 1 <= len(marker) <= 1
  • marker: alphabet lowercase
  • labels contains only lowercase letters.
  • marker contains only lowercase letters.
  • Types: labels is str, marker is str; result is int

Function signature

def find_last_label_position(labels: str, marker: str) -> int
labels str
The lowercase characters recorded in handling order; it may be empty.
marker str
The single lowercase character whose final position is requested.
returns int
The zero-based index of the final occurrence of marker in labels, or -1 if no occurrence exists.

Adapted from MBPP problem task_546 (CC BY 4.0). Rewritten, extended and verified by Iksha.

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