Apply Substitutions - 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.
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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code