Imagine you're building a template engine that needs to process text with dynamic placeholders! ๐ฏ
You are given a replacements mapping (dictionary/hash table) and a text string that may contain placeholders formatted as %var%, where each var corresponds to a key in the replacements mapping.
Here's the twist: replacement values can themselves contain placeholders! This creates a chain of substitutions that must be fully resolved.
Goal: Return the fully substituted text string with no remaining placeholders.
Example:text = "Hello %name%, welcome to %place%!"replacements = {"name": "%user%", "user": "Alice", "place": "Wonderland"}
Result: "Hello Alice, welcome to Wonderland!"
Input & Output
Visualization
Time & Space Complexity
Where n is text length and m is total length of all replacement values
Where k is the number of unique variables for memoization cache
Constraints
- 1 โค text.length โค 104
- 0 โค replacements.size โค 100
- 1 โค key.length, value.length โค 100
- Placeholder format: %variable_name% (alphanumeric and underscore only)
- No placeholder will exceed 50 characters including % symbols