Find the Greatest Document Token
Context
A text-indexing service reviews tokens extracted from a document and needs the lexicographically greatest token for a summary label. The tokens are already supplied as a list, so scan them without reordering the document data.
Problem
Implement a function that returns the lexicographically greatest token in the supplied list. Compare lowercase tokens in dictionary order, character by character. Start with a valid token as the current best, inspect every remaining token exactly once, and replace the best token whenever the current token is greater. If several tokens are equal and greatest, returning that same token is sufficient. The input is guaranteed to contain at least one token.
Examples
tokens = ["apple", "banana", "cherry", "date"]"date"tokens = ["cat", "catalog", "car", "can"]"catalog"tokens = ["zebra", "yellow", "zinc", "yak"]"zinc"Constraints
1 <= len(tokens) <= 1000001 <= len(tokens[i]) <= 30 for every itokens[i]: alphabet lowercasetokens contains only lowercase letters.- Types:
tokensis str[]; result is str
Function signature
def greatest_document_token(tokens: list[str]) -> str
tokenslist[str]- A non-empty list of lowercase tokens extracted from one document.
- returns str
- The lexicographically greatest token in tokens.
Adapted from MBPP problem task_418 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The input is non-empty, so a valid first token can initialize the retained candidate.
- Equal greatest tokens require no special tie-breaking.