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.

Input & Output

Example 1 — Basic Substitution
$ Input: replacements = {"name": "John", "place": "NYC"}, text = "Hello %name%, welcome to %place%!"
Output: "Hello John, welcome to NYC!"
💡 Note: Direct substitution: %name% becomes "John" and %place% becomes "NYC"
Example 2 — Nested Placeholders
$ Input: replacements = {"name": "Alice", "greeting": "Hi %name%!"}, text = "%greeting% How are you?"
Output: "Hi Alice! How are you?"
💡 Note: First %greeting% becomes "Hi %name%!", then %name% becomes "Alice"
Example 3 — No Replacements Needed
$ Input: replacements = {"name": "Bob"}, text = "Hello world!"
Output: "Hello world!"
💡 Note: Text contains no placeholders, so it remains unchanged

Constraints

  • 1 ≤ replacements.length ≤ 100
  • 1 ≤ text.length ≤ 1000
  • All replacement keys and values contain only alphanumeric characters
  • No circular dependencies in replacement values

Visualization

Tap to expand
Apply Substitutions ProcessInput Text:Hello %name%, welcome to %place%!Replacements:name → "John"place → "NYC"greeting → "Hi %name%!"SubstituteProcessing:Replace %name% → "John"Replace %place% → "NYC"Output:Hello John, welcome to NYC!
Understanding the Visualization
1
Input
Text with %var% placeholders and replacement mapping
2
Process
Resolve dependencies and substitute recursively
3
Output
Fully substituted text with no placeholders remaining
Key Takeaway
🎯 Key Insight: Use dependency resolution to handle nested placeholders efficiently
Asked in
Google 15 Microsoft 12 Amazon 8
12.3K Views
Medium Frequency
~15 min Avg. Time
487 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen