# Add Bold Tag in String

`String`, `Sweep Line`, `Interval`

Medium

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>"Input:
s = "aaabbcc"
dict = ["aaa","aab","bc"]
Output:
"<b>aaabbc</b>c"
``````

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

## Solution & Analysis

#### Merge Interval + Sweep Line

1. create a list of tuples/intervals with opening/closing positions, e.g. (open_index, close_index)
2. merge the list of intervals (see https://leetcode.com/problems/merge-intervals/)
3. go through the merged interval list and insert the tags into the string
##### Java String indexOf()

IIRC Java's implementation of `.indexOf()` is just the naive string matching algorithm, which is `O(n+m)` average and `O(n*m)` worst case.

12 ms, faster than 80.46%

``````public class Solution {
public class Interval{
int start;
int end;
Interval(int start,int end){
this.start = start;
this.end = end;
}
}
public String addBoldTag(String s, String[] dict) {
List<Interval> intervals = new ArrayList<>();
for(String str: dict) {
int index = s.indexOf(str, 0);
while(index != -1){
index = s.indexOf(str, index + 1);
}
}
Collections.sort(intervals,new Comparator<Interval>() {
public int compare(Interval a,Interval b){
return a.start - b.start;
}
});
List<Interval> mergedIntervals = mergeInterval(intervals);

int pre = 0;
StringBuilder sb = new StringBuilder();
for(Interval interval: mergedIntervals) {
sb.append(s.substring(pre, interval.start));
sb.append("<b>" + s.substring(interval.start, interval.end) + "</b>");
pre = interval.end;
}

if(pre < s.length()){
sb.append(s.substring(pre));
}

return sb.toString();
}

public List<Interval> mergeInterval(List<Interval> intervals) {
List<Interval> mergedIntervals = new ArrayList<>();
if(intervals == null || intervals.size() == 0) {
return mergedIntervals;
}

for(int i = 1; i<intervals.size(); i++){
Interval temp = intervals.get(i);
if(temp.start > mergedIntervals.get(mergedIntervals.size() - 1).end) {
} else {
int max = Math.max(mergedIntervals.get(mergedIntervals.size() - 1).end, temp.end);
mergedIntervals.get(mergedIntervals.size() - 1).end = max;
}
}
return mergedIntervals;
}
}
``````

33 ms, faster than 32.72%

``````class Solution {
String prefix = "<b>";
String suffix = "</b>";
public String addBoldTag(String s, String[] dict) {
if (s == null) {
return "";
}
int end = -1;
StringBuilder sb = new StringBuilder();
Trie trie = new Trie();
for (String word : dict) trie.insert(word);
for (int i = 0; i < s.length(); i++) {
if (end >= i) {
end = Math.max(end, trie.search(s, i));
} else {
end = trie.search(s, i);
if (end > i) {
sb.append(prefix);
}
}
if (end == i) sb.append(suffix);
sb.append(s.charAt(i));
}
if (end == s.length()) sb.append(suffix);
return sb.toString();
}

public static class Trie {
TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
if (word == null) return;
TrieNode node = root;
for (char c : word.toCharArray()) {
if (!node.map.containsKey(c)) {
node.map.put(c, new TrieNode());
}
node = node.map.get(c);
}
node.end = true;
}
public int search(String s, int index) {
TrieNode node = root;
int res = -1;
while (node != null && index < s.length()) {
node = node.map.getOrDefault(s.charAt(index++), null);
if (node != null && node.end == true) {
res = index;
}
}
return res;
}
}

public static class TrieNode {
Map<Character, TrieNode> map;
boolean end;
public TrieNode() {
map = new HashMap<>();
end = false;
}
}
}
``````