# 616 Add Bold Tag in String

## 616. [Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/description/)

## 1. Question

Given a string **s** and a list of strings **dict**, you need to add a closed pair of bold tag`<b>`and`</b>`

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:**

```
Input:
s = "abcxyz123"
dict = ["abc","123"]

Output:
"<b>abc</b>xyz<b>123</b>"
```

**Example 2:**

```
Input:

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

Output:
"<b>aaabbc</b>c"
```

**Note:**

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

## 2. Implementation

**(1) Brute Force**

```java
class Solution {
    public String addBoldTag(String s, String[] dict) {
        if (dict == null || dict.length == 0) {
            return s;
        }

        int n = s.length();
        boolean[] addBold = new boolean[n];

        for (int i = 0, end = 0; i < s.length(); i++) {
            for (String word : dict) {
                if (s.startsWith(word, i)) {
                    end = Math.max(i + word.length(), end);
                }
            }
            addBold[i] = end > i;
        }

        StringBuilder res = new StringBuilder();
        for (int i = 0; i < n; i++) {
            if (!addBold[i]) {
                res.append(s.charAt(i));
                continue;
            }

            int j = i;
            while (j < n && addBold[j]) j++;
            res.append("<b>").append(s.substring(i, j)).append("</b>");
            i = j - 1;
        }
        return res.toString();
    }
}
```

(2) Optimization

上一种做法是对于S的每个位置i，我们都尝试每个dict的word。其实我们只要将dict的word，通过indexOf找出word在s的位置index，然后将相应的区间在bold数组里mark成true即可

```java
class Solution {
    public String addBoldTag(String s, String[] dict) {
        if (dict == null || dict.length == 0) {
            return s;
        }

        int n = s.length();
        boolean[] bold = new boolean[n];

        for (int i = 0; i < dict.length; i++) {
            int index = s.indexOf(dict[i]);

            while (index != -1) {
                for (int j = 0; j < dict[i].length(); j++) {
                    bold[index + j] = true;
                }

                index = s.indexOf(dict[i], index + 1);
            }
        }

        StringBuilder res = new StringBuilder();
        for (int i = 0; i < n; i++) {
            if (!bold[i]) {
                res.append(s.charAt(i));
                continue;
            }
            int j = i;
            while (j < n && bold[j]) ++j;
            res.append("<b>").append(s.substring(i, j)).append("</b>");
            i = j - 1;
        }
        return res.toString();
    }
}
```

## 3. Time & Space Complexity


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/google/616-add-bold-tag-in-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
