Trains the technique from
LeetCode 3481Apply SubstitutionsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A deployment tool renders a config template. text is the template and replacements is a list of rules; rule replacements[i] is a pair [name, body] saying that the placeholder named name stands for the string body.
A placeholder is written as a rule name wrapped in braces, for example {PORT}. Braces are used for nothing else: in text and in every body, each { opens a placeholder that the very next } closes, and the name between them is the name of exactly one rule. Rule names are distinct and are made of uppercase letters, digits and underscores.
A body may itself contain placeholders, and those must be rendered too, as must any placeholders that appear in their bodies, and so on until no braces are left. The rules come in no particular order, so a body may name a rule that is listed later or earlier, and some rules may go unused. No rule ever depends on itself, directly or through a chain of other rules, so the rendering always finishes.
Return the rendered template.
Example 1
`{HOST}` stands for `db.{ZONE}.internal`, whose own `{ZONE}` stands for `eu-west`, so the placeholder renders as `db.eu-west.internal` and the rest of the template is copied through unchanged.
Example 2
Here the body of the second rule names the first rule, so `{LOGS}` renders as `/srv/logs`. Both appearances of the placeholder are rendered.
Example 3
The template holds no braces, so nothing is replaced and the single rule goes unused.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def apply_substitutions(replacements: list[list[str]], text: str) -> str:public String applySubstitutions(String[][] replacements, String text)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.