Similar Problems
Similar Problems not available
Add Bold Tag In String - Leetcode Solution
Companies:
LeetCode: Add Bold Tag In String Leetcode Solution
Difficulty: Medium
Topics: string hash-table array
Problem statement: Given a string s and an integer array startIndices of the same length.
The task is to concatenate all substrings in s which start at indices specified in startIndices, sorted in ascending order, and wrap them with bold tag <b> and </b>.
If two substrings overlap, you should wrap them together by only one pair of bold tags. If two consecutive substrings are separated by more than one character, you should place the bold tags separately.
Return the final string after the aforementioned operations.
Example 1:
Input: s = "abcxyz123", startIndices = [0, 6], Output: "<b>abcxyz</b>123" Explanation:
- substrings starting at index 0: "abcxyz"
- substrings starting at index 6: "123"
- concatenate substrings "abcxyz" and "123" together and wrap them with bold tag "<b>" and "</b>".
Example 2:
Input: s = "aaabbcc", startIndices = [0, 1, 4, 5], Output: "<b>aaab</b>b<b>cc</b>" Explanation:
- substrings starting at index 0: "aaa"
- substrings starting at index 1: "aa"
- substrings starting at index 4: "bcc"
- substrings starting at index 5: "c"
- concatenate substrings "aaab" and "cc" together and wrap them with bold tag "<b>" and "</b>".
Constraints: 1 <= s.length <= 1000 0 <= startIndices.length <= 100 0 <= startIndices[i] <= s.length - 1 startIndices are distinct.
Approach: This problem can be solved using string manipulation and concept of merging overlapped or adjacent substrings. We will start iterating over the startIndices array and extract the substring from the given string s starting from the current index i up to the index mentioned in the startIndices array.
While extracting the substrings, we need to merge the adjacent substrings if they overlap or are separated by only one character. While doing so, we need to keep track of the starting and ending indices of the merged substrings in order to enclose them with the bold tag <b> and </b>.
For this, we can initialize a variable bold to an array of booleans of length equal to the size of the given string. Loop through the startIndices array and mark the corresponding positions in the bold array as true. Then loop through the bold array and merge all the contiguous true values into one substring. We will then add the bold tag to these substrings and store them in a list.
We will then concatenate all the substrings stored in the list to get the final string after applying the above operations.
Algorithm:
- Initialize an empty list boldSubstrings to store the bold substrings.
- Initialize a boolean array bold to mark the positions of the bold substrings and initialize all its values to false.
- Loop through the startIndices array and mark the corresponding positions in the bold array as true.
- Initialize variables startIndex and endIndex to -1 for keeping track of the start and end positions of the merged substrings.
- Loop through the bold array and merge all the contiguous true values into one substring.
- If the current value of bold[i] is true and the startIndex is -1, then set the startIndex to i.
- If the current value of bold[i] is false and startIndex is not -1, then set the endIndex to i-1 and add the bold tag to the substring starting from startIndex to endIndex. Add this substring to the list boldSubstrings and set the startIndex to -1.
- If there are any remaining true values in the bold array, then set the endIndex to the last index of the string and add the bold tag to the substring starting from startIndex to endIndex. Add this substring to the list boldSubstrings.
- Return the final string obtained by joining all the substrings in the list using the Python join() method.
Python Code:
def addBoldTag(s, startIndices): n = len(s) bold = [False] * n
for startIndex in startIndices:
bold[startIndex] = True
boldSubstrings = []
startIndex, endIndex = -1, -1
for i in range(n):
if bold[i] and startIndex == -1:
startIndex = i
if not bold[i] and startIndex != -1:
endIndex = i-1
boldSubstrings.append("<b>" + s[startIndex:endIndex+1] + "</b>")
startIndex = -1
if startIndex != -1:
endIndex = len(s)-1
boldSubstrings.append("<b>" + s[startIndex:endIndex+1] + "</b>")
return "".join(boldSubstrings)
Testing the code with the given test cases
print(addBoldTag("abcxyz123", [0, 6])) #"<b>abcxyz</b>123" print(addBoldTag("aaabbcc", [0, 1, 4, 5])) #"<b>aaab</b>b<b>cc</b>"
Add Bold Tag In String Solution Code
1