Clean a Product Label
Context
An ecommerce catalog stores product labels that may contain characters needing masking before display. Given a label, a set of characters to mask, and one substitute character, create the display-safe label while preserving all other text exactly, including spaces and repeated characters.
Problem
Write a function that transforms a product label one character at a time. If the current character appears in the supplied list of characters to mask, append the substitute character to the result. Otherwise, append the original character. Preserve the original order, keep the result the same length as the label, and process every occurrence of every target character. Spaces and characters not listed for masking must remain unchanged.
Examples
label = "Apple Juice"mask_chars = ["p", "e"]substitute = "*""A**l* Juic*"label = "A1B-200"mask_chars = ["1", "2", "0"]substitute = "#""A#B-###"label = "Fresh mint"mask_chars = ["m", "t"]substitute = "_""Fresh _in_"Constraints
0 <= len(label) <= 10001 <= len(mask_chars) <= 261 <= len(mask_chars[i]) <= 1 for every imask_chars: distinct elements1 <= len(substitute) <= 1- Types:
labelis str,mask_charsis str[],substituteis str; result is str
Function signature
def sanitize_product_label(label: str, mask_chars: list[str], substitute: str) -> str
labelstr- The product label to transform; it may be empty and may contain spaces.
mask_charslist[str]- Distinct one-character strings that must be replaced.
substitutestr- The single character used for every replacement.
- returns str
- The transformed product label, with each listed character replaced by the substitute while all other characters retain their order and value.
Adapted from MBPP problem task_732 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The target collection is represented as a list so its members are distinct and each member is exactly one character.