# Embolden - Google Top Interview Questions

### Problem Statement :

```Given a string text and a list of strings patterns, implement an embolden function where all substrings in text that match any string in patterns are wrapped in <b> and </b> tags.
If two patterns are adjacent or overlap, they should be merged into one tag.

Constraints

n < 5000 where n is the length of text

m < 100 where m is the length of patterns

Example 1

Input

text = "abcdefg"

patterns = ["bc", "ef"]

Output

"a<b>bc</b>d<b>ef</b>g"

Explanation

bc and ef match the text and are wrapped in <b> and </b> tags.

Example 2

Input

text = "abcdefg"

patterns = ["bc", "de"]

Output

"a<b>bcde</b>fg"

Explanation

If two patterns are adjacent or overlap, they should be merged into one tag.```

### Solution :

```                        ```Solution in C++ :

string solve(string text, vector<string>& patterns) {
vector<vector<int>> intervals;
int N = text.size();
for (int i = 0; i < N; i++) {
for (auto& p : patterns) {
if (p.size() <= N - i && text.compare(i, p.size(), p) == 0) {
if (intervals.empty()) {
intervals.push_back({i, i + p.size() - 1});
} else {
if (intervals.back()[1] >= i - 1)
intervals.back()[1] = i + p.size() - 1;
else
intervals.push_back({i, i + p.size() - 1});
}
}
}
}

string ans;
int idx = 0;
for (int i = 0; i < intervals.size(); i++) {
ans.append(text.substr(idx, intervals[i][0] - idx));
ans.append("<b>");
ans.append(text.substr(intervals[i][0], intervals[i][1] - intervals[i][0] + 1));
ans.append("</b>");
idx = intervals[i][1] + 1;
}
if (idx < text.size()) {
ans.append(text.substr(idx));
}
return ans;
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, text, patterns):
n = len(text)
bolded = [False] * n

for pat in patterns:
start = text.find(pat)

while start != -1:
for i in range(start, start + len(pat)):
bolded[i] = True

start = text.find(pat, start + 1)

ans = []
idx = 0

while idx < n:
if bolded[idx]:
ans.append("<b>")

while idx < n and bolded[idx]:
ans.append(text[idx])
idx += 1

ans.append("</b>")
else:
ans.append(text[idx])
idx += 1

return "".join(ans)```
```

## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

## Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

## Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

## Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C