PracticeData Structures

Smallest Token Lengths

MediumData Structurestraversalstate trackingcomparison Time not estimated Not started

Context

A document indexer summarizes short words for a compact report. Given the tokens from one document and a requested count, identify the shortest token lengths. Repeated lengths must remain repeated because each token is a separate document item.

Problem

Write a function that receives a list of document tokens and a requested count. Compute each token's length and select at most the requested number of smallest lengths. Return the selected lengths in nondecreasing order. Each occurrence counts separately: if several tokens have the same length, enough copies must be retained when they rank among the requested smallest values. If the requested count is zero, return an empty list. The input and request are constrained so the request never exceeds the number of tokens.

Examples

Example 1
Input: limit = 2tokens = ["cat", "elephant", "ox", "rabbit"]
Output: [2, 3]
Explanation: The token lengths are 3, 8, 2, and 6, and the two smallest returned lengths are [2, 3].
Example 2
Input: limit = 3tokens = ["apple", "kiwi", "pear", "plum"]
Output: [4, 4, 4]
Explanation: The three smallest lengths are all 4, so the executed result is [4, 4, 4].
Example 3
Input: limit = 0tokens = ["sun", "moon", "star"]
Output: []
Explanation: The requested count is zero, so the executed result is an empty list: [].

Constraints

  • 0 <= len(tokens) <= 10000
  • 1 <= len(tokens[i]) <= 40 for every i
  • tokens[i]: alphabet lowercase
  • 0 <= limit <= len(tokens)
  • Each token contains lowercase English letters only.
  • Types: tokens is str[], limit is int; result is int[]

Function signature

def smallest_token_lengths(tokens: list[str], limit: int) -> list[int]
tokens list[str]
The document tokens whose character lengths are candidates.
limit int
The maximum number of token lengths to return.
returns list[int]
The lengths of the limit shortest tokens, or all available lengths when fewer exist, arranged in nondecreasing order.

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

Notes

  • The returned list may be empty only when limit is zero or the token list is empty.
  • Equal-length tokens retain their multiplicity.
Approved · c002-abs_974_v114 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1