PracticeStrings
Last Label Checkpoint
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:
6Explanation: 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:
-1Explanation: 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:
-1Explanation: 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) <= 1000labels: alphabet lowercase1 <= len(marker) <= 1marker: alphabet lowercaselabels contains only lowercase letters.marker contains only lowercase letters.- Types:
labelsis str,markeris str; result is int
Function signature
def find_last_label_position(labels: str, marker: str) -> int
labelsstr- The lowercase characters recorded in handling order; it may be empty.
markerstr- 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.
Code
Saved