This question evaluates understanding of string processing, dependency resolution, recursive expansion, cycle detection, and efficiency techniques such as memoization for nested template substitution.
You are given a dictionary (map) from keys to string templates. Templates may reference other keys using placeholders of the form %KEY%.
Example dictionary:
X -> "a"
Y -> "b"
Z -> "%X% and %Y%"
Given an input string that may also contain placeholders (e.g., "%X% and %Z%"), output the fully substituted string.
For the example above:
"%X% and %Z%"
"a and a and b"
%W%
not in dictionary)
A -> "%B%"
,
B -> "%A%"
)