Apply Substitutions
Real-world version: Render A Config TemplateThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- replacements = [["HOST", "db.{ZONE}.internal"], ["ZONE", "eu-west"]], text = "url=db_at_{HOST}:5432"
- Output
- "url=db_at_db.eu-west.internal:5432"
`{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
- Input
- replacements = [["ROOT", "/srv"], ["LOGS", "{ROOT}/logs"]], text = "{LOGS} then {LOGS}"
- Output
- "/srv/logs then /srv/logs"
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
- Input
- replacements = [["A1", "keep"]], text = "no braces at all"
- Output
- "no braces at all"
The template holds no braces, so nothing is replaced and the single rule goes unused.
Constraints
- 1 <= replacements.length <= 100
- replacements[i].length == 2
- 1 <= replacements[i][j].length <= 100
- 1 <= text.length <= 1000
- Rule names are distinct, and every placeholder names one of the rules.
- No rule depends on itself directly or through a chain of other rules.
- The rendered template is at most 5000 characters long.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def apply_substitutions(replacements: list[list[str]], text: str) -> str:public String applySubstitutions(String[][] replacements, String text)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's