Add Bold Tag 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 string s and a list of strings dict, you need to add a closed pair of bold tag and to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. Also, if two substrings wrapped by bold tags are consecutive, you need to combine them.

Example 1:


s = "abcxyz123"
dict = ["abc","123"]

Output: "abcxyz123"

Example 2:


s = "aaabbcc"
dict = ["aaa","aab","bc"]

Output: "aaabbcc"


  • The given dict won't contain duplicates, and its length won't exceed 100.
  • All the strings in input have length in range [1, 1000].

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: