Alphabetize Each Document's Tokens
Context
A document archive stores a short label for each document and the tokens extracted from it. The archive keeps labels in a fixed order, with each token list paired to the label at the same position. Clean the archive by alphabetizing every document's tokens without mixing documents or removing repeated tokens.
Problem
Write a function that receives document labels and their token lists in matching order. For every position, sort that document's tokens into nondecreasing lexicographic order. Return the sorted token lists in the same order, so position i still belongs to the original label at position i. Preserve every token occurrence, including duplicates. Labels are identifiers only and must not appear in the returned lists. The input lists are paired and have equal outer lengths; either outer list may be empty, and an individual document may have no tokens.
Examples
labels = ["invoice", "memo", "report"]token_lists = [["total", "due", "amount"], ["draft", "final", "draft"], ["summary", "annual", "data"]][["amount", "due", "total"], ["draft", "draft", "final"], ["annual", "data", "summary"]]labels = ["alpha", "beta"]token_lists = [["zebra", "apple", "kiwi", "apple"], []][["apple", "apple", "kiwi", "zebra"], []]labels = []token_lists = [][]Constraints
0 <= len(labels) <= 401 <= len(labels[i]) <= 16 for every ilabels: distinct elementslabels[i]: alphabet lowercase0 <= len(token_lists) <= len(labels)0 <= len(token_lists[i]) <= 30 for every i1 <= len(token_lists[i][j]) <= 20 for every i, jtoken_lists[i][j]: alphabet lowercaselabels and token_lists contain only lowercase letterstoken_lists contains no spaces or punctuation.- Types:
labelsis str[],token_listsis str[][]; result is str[][]
Function signature
def sort_document_tokens(labels: list[str], token_lists: list[list[str]]) -> list[list[str]]
labelslist[str]- Lowercase document identifiers, in archive order.
token_listslist[list[str]]- Token sequences paired with labels by position.
- returns list[list[str]]
- The token lists in the original document order, with each individual list sorted lexicographically in nondecreasing order and retaining all original occurrences.
Adapted from MBPP problem task_662 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The two outer inputs always have equal lengths, pairing each label with one token sequence.
- An empty token sequence remains empty.
- The returned outer list follows the original label order.