Skip to main content
LeetCode 3481, Medium. Topics: Depth-First Search, Breadth-First Search, Graph, Topological Sort, Array, Hash Table, String. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 13 parametrized pytest cases, and a playground notebook:

Problem

You are given a replacements mapping and a text string that may contain placeholders formatted as %var%, where each var corresponds to a key in the replacements mapping. Each replacement value may itself contain one or more such placeholders. Each placeholder is replaced by the value associated with its corresponding replacement key. Return the fully substituted text string which does not contain any placeholders.

Examples

Constraints

  • 1 <= replacements.length <= 10
  • Each element of replacements is a two-element list [key, value], where:
    • key is a single uppercase English letter.
    • value is a non-empty string of at most 8 characters that may contain zero or more placeholders formatted as %<key>%.
  • All replacement keys are unique.
  • The text string is formed by concatenating all key placeholders (formatted as %<key>%) randomly from the replacements mapping, separated by underscores.
  • text.length == 4 * replacements.length - 1
  • Every placeholder in the text or in any replacement value corresponds to a key in the replacements mapping.
  • There are no cyclic dependencies between replacement keys.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026