Bold Words In String is an example of string problems. In this post we will see how we can solve it in Javascript.

Problem Description

Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any letters between and tags become bold.

The returned string should use the least number of tags possible, and of course the tags should form a valid combination.

For example, given that words = ["ab", "bc"] and S = "aabcd", we should return "aabcd".

Note that returning "aabcd" would use more tags, so it is incorrect.


  • words has length in range [0, 50].
  • words[i] has length in range [1, 10].
  • S has length in range [0, 500].
  • All characters in words[i] and S are lowercase letters.

 Solution: Please check the main.js snippet for the solution. If you have different approach in mind or have any suggestion for this implementation feel free to share in the comment below. Thanks!

Originally posted at: