616. Add Bold Tag in String

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"
```

Constraints:

• 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].

Note: This question is the same as 758: https://leetcode.com/problems/bold-words-in-string/

616. Add Bold Tag in String
``````struct Solution;

impl Solution {
fn add_bold_tag(s: String, dict: Vec<String>) -> String {
let n = s.len();
let mut tag: Vec<bool> = vec![false; n];
for p in dict {
let mut start = 0;
let m = p.len();
while let Some(offset) = s[start..].find(&p) {
for i in 0..m {
tag[start + offset + i] = true;
}
start += offset + 1;
}
}
let s: Vec<char> = s.chars().collect();
let mut res = "".to_string();
let mut in_tag = false;
for i in 0..n {
if in_tag != tag[i] {
res += if in_tag { "</b>" } else { "<b>" };
in_tag = !in_tag;
}
res.push(s[i]);
}
if in_tag {
res += "</b>";
}
res
}
}

#[test]
fn test() {
let s = "abcxyz123".to_string();
let dict = vec_string!["abc", "123"];
let res = "<b>abc</b>xyz<b>123</b>".to_string();