Problem · String

Substitute Templates

MediumGoogleONSITE INTERVIEW
See Google hiring insights

Write a tool which supports substitutions of templates by other strings.

Function Description

Complete the function substituteTemplates in the editor.

substituteTemplates has the following parameters:

  1. 1. Map substitutions: a map containing the substitutions
  2. 2. String template: the template string to be resolved

Returns

String: the resolved template

Examples
01 · Example 1
substitutions = {"X" -> "123", "Y" -> "456", "Z" -> "abc"}
template = "%X%_%Y%"
return = "123_456"
The template %X%_%Y% should be resolved by replacing %X% with 123 and %Y% with 456, resulting in 123_456.
02 · Example 2
substitutions = {"X" -> "123", "Y" -> "456", "Z" -> "abc%Y%"}
template = "%X%_%Y%_%Z%"
return = "123_456_abc456"
The template %X%_%Y%_%Z% should be resolved by replacing %X% with 123, %Y% with 456, and %Z% with abc%Y% which further resolves to abc456. The final result is 123_456_abc456.
More Google problems
drafts saved locally
public String substituteTemplates(Map<String, String> substitutions, String template) {
  // write your code here
}
substitutions{"X" -> "123", "Y" -> "456", "Z" -> "abc"}
template"%X%_%Y%"
expected"123_456"
checking account