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

example_1.py โ€” Basic Substitution
$ Input: text = "Hello %name%!" replacements = {"name": "World"}
โ€บ Output: "Hello World!"
๐Ÿ’ก Note: Simple case where %name% is replaced with "World"
example_2.py โ€” Nested Substitution
$ Input: text = "Hello %name%, welcome to %place%!" replacements = {"name": "%user%", "user": "Alice", "place": "Wonderland"}
โ€บ Output: "Hello Alice, welcome to Wonderland!"
๐Ÿ’ก Note: %name% resolves to %user% which then resolves to "Alice". %place% directly resolves to "Wonderland"
example_3.py โ€” No Replacements Needed
$ Input: text = "No placeholders here" replacements = {"unused": "value"}
โ€บ Output: "No placeholders here"
๐Ÿ’ก Note: When text contains no placeholders, it's returned unchanged

Visualization

Tap to expand
Hello %name%, welcome to %place%!Original TextReplacementsname โ†’ %user%user โ†’ Aliceplace โ†’ WonderlandVariable MappingnameuserplaceAliceWonder-landDependency Graph
Understanding the Visualization
1
Parse Input
Extract placeholders from text and build replacement mapping
2
Build Dependency Graph
Create relationships between variables that depend on each other
3
Resolve Variables
Use DFS with memoization to resolve each variable to its final value
4
Apply Substitutions
Replace all placeholders in the original text with resolved values
Key Takeaway
๐ŸŽฏ Key Insight: By modeling variable dependencies as a graph and using memoization, we can efficiently resolve complex nested substitutions while handling edge cases like circular dependencies.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Where n is text length and m is total length of all replacement values

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is the number of unique variables for memoization cache

n
2n
โœ“ Linear Space

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
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~25 min Avg. Time
890 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