631. Apply Substitutions
You are provided with a replacements mapping and a text string that can contain placeholders written in the form %var%, where each var matches a key in the replacements mapping. The replacement value for a key may also include one or more placeholders. Substitute each placeholder with the value mapped to its corresponding replacement key.
Return the fully substituted text string which does not contain any placeholders.
Example 1:
Input:Â replacements = [["A" , "bce"] , ["B" , "ace"] , ["C" , "abc%B%"]] , text = "%A%_%B%_%C%"
Output:Â "bce_ace_abcace"
Explanation:
- The mapping associates "A" with "bce" , "B" with "ace" , and "C" with "abc%B%".
- Replace %A% with "bce" and %B% with "ace" in the text.
- Then, for %C%, substitute %B% in "abc%B%" with "ace" to obtain "abcace".
- The final text becomes "bce_ace_abcace".
Example 2:
Input:Â replacements = [["A" , "zac"] , ["B" , "def"]] , text = "%A%_%B%"
Output:Â "zac_def"
Explanation:
- The mapping associates "A" with "zac" and "B" with "def".
- Replace %A% with "zac" and %B% with "def" in the text.
- The final text becomes "abc_def".
Now Your Turn!
Pick the correct output for the given inputInput: replacements = [ ["A", "ab"], ["B", "wow"], ["C", "Sup%A%"] ], text = "%C%_%C%_%B%"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= replacements.length <= 10
- Each element of replacements is a two-element list [key, value], where:
- key is a single uppercase English letter.
- value is a non-empty string of at most 8 characters that may contain zero or more placeholders formatted as %<key>%.
- All replacement keys are unique.
- The text string is formed by concatenating all key placeholders (formatted as %<key>%) randomly from the replacements mapping, separated by underscores.
- text.length == 4 * replacements.length - 1
- Every placeholder in the text or in any replacement value corresponds to a key in the replacements mapping.
- There are no cyclic dependencies between replacement keys.