> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Add Bold Tag in String Python Solution

> Tested Python solution for LeetCode 616 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 616, [Medium](/catalog/medium). Topics: [Trie](/catalog/topics/trie), [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [String Matching](/catalog/topics/string-matching). [View on LeetCode](https://leetcode.com/problems/add-bold-tag-in-string/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 616   # by problem number
lcpy gen -s add_bold_tag_in_string   # by problem name
```

## Problem

You are given a string `s` and an array of strings `words`.

You should add a closed pair of bold tag `<b>` and `</b>` to wrap the substrings in `s` that exist in `words`.

* If two such substrings overlap, you should wrap them together with only one pair of closed bold-tag.
* If two substrings wrapped by bold tags are consecutive, you should combine them.

Return `s` after adding the bold tags.

### Examples

```
Input: s = "abcxyz123", words = ["abc","123"]
Output: "<b>abc</b>xyz<b>123</b>"
Explanation: The two strings of words are substrings of s as following: "abcxyz123". We add <b> before each substring and </b> after each substring.
```

```
Input: s = "aaabbb", words = ["aa","b"]
Output: "<b>aaabbb</b>"
```

### Constraints

* `1 <= s.length <= 1000`
* `0 <= words.length <= 100`
* `1 <= words[i].length <= 1000`
* `s` and `words[i]` consist of English letters and digits.
* All the values of `words` are **unique**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_bold_tag_in_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_bold_tag_in_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(|s| * sum|words| * |s|) with find; effectively O(n * m)
    # Space: O(n)
    def add_bold_tag(self, s: str, words: list[str]) -> str:
        mask = [False] * len(s)
        for word in words:
            start = s.find(word)
            while start != -1:
                for i in range(start, start + len(word)):
                    mask[i] = True
                start = s.find(word, start + 1)
        out: list[str] = []
        for i, ch in enumerate(s):
            if mask[i] and (i == 0 or not mask[i - 1]):
                out.append("<b>")
            out.append(ch)
            if mask[i] and (i == len(s) - 1 or not mask[i + 1]):
                out.append("</b>")
        return "".join(out)
```

## Complexity

| Time | Space | | | | | | |
| - | - | - | - | - | - | - | - |
| O( | s | \* sum | words | \* | s | ) with find; effectively O(n \* m) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.