616. Add Bold Tag in String
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to wrap substrings of a given string s with bold tags and if those substrings are present in a dictionary words. The key challenges are to merge overlapping and consecutive bolded segments into a single, larger bolded segment.
Solution
Add Bold Tag in String in Python
class Solution:
def addBoldTag(self, s: str, words: List[str]) -> str:
n = len(s)
if n == 0:
return ""
# 1. Create a boolean list to mark characters that should be bold.
bold = [False] * n
# 2. For each word, find all its occurrences in s and mark the corresponding range in the bold list.
for word in words:
start = s.find(word, 0)
while start != -1:
for i in range(start, start + len(word)):
bold[i] = True
start = s.find(word, start + 1)
# 3. Build the result string by iterating through s and the bold list.
result = []
i = 0
while i < n:
if bold[i]:
result.append("<b>")
j = i
while j < n and bold[j]:
result.append(s[j])
j += 1
result.append("</b>")
i = j
else:
result.append(s[i])
i += 1
return "".join(result)
Add Bold Tag in String in Java
class Solution {
public String addBoldTag(String s, String[] words) {
int n = s.length();
if (n == 0) {
return "";
}
// 1. Create a boolean array to mark characters that should be bold.
boolean[] bold = new boolean[n];
// 2. For each word, find all its occurrences in s and mark the corresponding range in the bold array.
for (String word : words) {
int start = s.indexOf(word, 0);
while (start != -1) {
for (int i = start; i < start + word.length(); i++) {
bold[i] = true;
}
start = s.indexOf(word, start + 1);
}
}
// 3. Build the result string by iterating through s and the bold array.
StringBuilder result = new StringBuilder();
for (int i = 0; i < n; i++) {
// If the current character is bold and it's the beginning of a bold section
if (bold[i] && (i == 0 || !bold[i - 1])) {
result.append("<b>");
}
result.append(s.charAt(i));
// If the current character is bold and it's the end of a bold section
if (bold[i] && (i == n - 1 || !bold[i + 1])) {
result.append("</b>");
}
}
return result.toString();
}
}
Add Bold Tag in String in C++
class Solution {
public:
string addBoldTag(string s, vector<string>& words) {
int n = s.length();
if (n == 0) {
return "";
}
// 1. Create a boolean array to mark characters that should be bold.
vector<bool> bold(n, false);
// 2. For each word, find all its occurrences in s and mark the corresponding range in the bold array.
for (const string& word : words) {
size_t start = s.find(word, 0);
while (start != string::npos) {
for (int i = start; i < start + word.length(); ++i) {
bold[i] = true;
}
start = s.find(word, start + 1);
}
}
// 3. Build the result string by iterating through s and the bold array.
string result = "";
for (int i = 0; i < n; ++i) {
// If the current character is bold and it's the beginning of a bold section
if (bold[i] && (i == 0 || !bold[i - 1])) {
result += "<b>";
}
result += s[i];
// If the current character is bold and it's the end of a bold section
if (bold[i] && (i == n - 1 || !bold[i + 1])) {
result += "</b>";
}
}
return result;
}
};
Add Bold Tag in String in TypeScript
function addBoldTag(s: string, words: string[]): string {
const n = s.length;
if (n === 0) {
return "";
}
// 1. Create a boolean array to mark characters that should be bold.
const bold = new Array(n).fill(false);
// 2. For each word, find all its occurrences in s and mark the corresponding range in the bold array.
for (const word of words) {
let start = s.indexOf(word, 0);
while (start !== -1) {
for (let i = start; i < start + word.length; i++) {
bold[i] = true;
}
start = s.indexOf(word, start + 1);
}
}
// 3. Build the result string by iterating through s and the bold array.
let result = "";
for (let i = 0; i < n; i++) {
// If the current character is bold and it's the beginning of a bold section
if (bold[i] && (i === 0 || !bold[i - 1])) {
result += "<b>";
}
result += s[i];
// If the current character is bold and it's the end of a bold section
if (bold[i] && (i === n - 1 || !bold[i + 1])) {
result += "</b>";
}
}
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.