Easy 0 solved 0% acceptance

Mad Lib Builder

Fill the bracketed blanks of a story template with words, in order.

What you'll learn

  • Find bracketed parts inside a string
  • Replace them in order with list items
  • Handle running out of replacement words

Problem

In a word game, one player writes a story with blanks and the others call out words without seeing the story. A blank is written as a hint in square brackets, such as [noun] or [number]. Replace the first blank with the first word, the second blank with the second word, and so on, brackets and hint included. If there are fewer words than blanks, the blanks left over stay exactly as they are; extra words are not used.

Implement mad_lib_builder(template, words).

Input

template: The story. Every [ is closed by a later ], with no brackets nested inside a blank.

words: The words to put in the blanks, in order.

Output

Return the finished story.

Constraints

0 ≤ length of template ≤ 1000

0 ≤ length of words ≤ 100, each word has at most 30 characters and no square brackets

Examples

Example 1
Input:
template = "My [animal] ate [number] [food] for breakfast."
words = ["hamster", "12", "pancakes"]

Expected output:
expected = "My hamster ate 12 pancakes for breakfast."
Input:
std::string template = "My [animal] ate [number] [food] for breakfast.";
std::vector<std::string> words = {"hamster", "12", "pancakes"};

Expected output:
std::string expected = "My hamster ate 12 pancakes for breakfast.";
Input:
string template = "My [animal] ate [number] [food] for breakfast.";
string[] words = new string[] {"hamster", "12", "pancakes"};

Expected output:
string expected = "My hamster ate 12 pancakes for breakfast.";

Explanation

Each blank takes the next word: "My hamster ate 12 pancakes for breakfast.".

Example 2
Input:
template = "The [adjective] [noun] sang."
words = ["sleepy"]

Expected output:
expected = "The sleepy [noun] sang."
Input:
std::string template = "The [adjective] [noun] sang.";
std::vector<std::string> words = {"sleepy"};

Expected output:
std::string expected = "The sleepy [noun] sang.";
Input:
string template = "The [adjective] [noun] sang.";
string[] words = new string[] {"sleepy"};

Expected output:
string expected = "The sleepy [noun] sang.";

Explanation

Only one word was given, so the second blank stays: "The sleepy [noun] sang.".

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Scan the template character by character and copy normal characters straight to the result.

Hint 2

When you see `[`, find the matching `]` and treat everything in between as one blank.

Hint 3

Keep a counter of blanks seen; if it reaches the number of words, copy the blank unchanged.

Expected complexity

O(n + total word length) time, O(n) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

The best passing solution of each coder. Faster, smaller and better-reviewed solutions rank higher.

No passing solutions yet. Solve it to take first place.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.