Tap here to switch tabs
Problem
Submissions

Problem: Apply Substitutions

Try to solve the Apply Substitutions problem.

Statement

You are given a list of substitution rules replacements and a string text.

Each rule in replacements is a pair [key, value], indicating that any placeholder in the format "%<key>%" should be replaced with its corresponding value. A value may itself contain zero or more placeholders referencing other keys.

Return the final string after fully substituting all placeholders in text and in any nested value strings until no placeholders remain.

Constraints:

  • 1≤1 \leq replacements.length ≤10\leq 10

  • Each element of replacements is a list of 22 elements [key, value].

  • key is a single uppercase English letter.

  • 1≤1 \leq value.length ≤8\leq 8

  • value consists of characters and optional placeholders formatted as "%<key>%".

  • All key values in replacements are unique.

  • text is formed by concatenating all key placeholders from replacements in some order, separated by underscores (_).

  • text.length =4×= 4 \times replacements.length −1- 1

  • Every placeholder in text or in any value corresponds to a valid key in replacements.

  • There are no cyclic dependencies among replacement keys.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Apply Substitutions

Try to solve the Apply Substitutions problem.

Statement

You are given a list of substitution rules replacements and a string text.

Each rule in replacements is a pair [key, value], indicating that any placeholder in the format "%<key>%" should be replaced with its corresponding value. A value may itself contain zero or more placeholders referencing other keys.

Return the final string after fully substituting all placeholders in text and in any nested value strings until no placeholders remain.

Constraints:

  • 1≤1 \leq replacements.length ≤10\leq 10

  • Each element of replacements is a list of 22 elements [key, value].

  • key is a single uppercase English letter.

  • 1≤1 \leq value.length ≤8\leq 8

  • value consists of characters and optional placeholders formatted as "%<key>%".

  • All key values in replacements are unique.

  • text is formed by concatenating all key placeholders from replacements in some order, separated by underscores (_).

  • text.length =4×= 4 \times replacements.length −1- 1

  • Every placeholder in text or in any value corresponds to a valid key in replacements.

  • There are no cyclic dependencies among replacement keys.