
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Add Bold Tag in String in C++
Suppose we have a string s and a list of strings called dict, we have to add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in that dict. When two such substrings overlap, then we have to wrap them together by only one pair of the closed bold tags. Also, if two substrings wrapped by bold tags are consecutive, we need to combine them.
So, if the input is like s = "abcxyz123" dict is ["abc","123"], then the output will be "<b>abc</b>xyz<b>123</b>"
To solve this, we will follow these steps −
n := size of s
Define an array bold of size n
ret := blank string
for initialize i := 0, end := 0, when i < size of s, update (increase i by 1), do −
for initialize j := 0, when j < size of dict, update (increase j by 1), do −
if substring of s from index (i to size of dict[j] - 1) is same as dict[j], then −
end := maximum of end and i + size of dict[j]
bold[i] := end > i
for initialize i := 0, when i < size of s, update i = j, do −
if bold[i] is zero, then −
ret := ret + s[i]
j := i + 1
Ignore following part, skip to the next iteration
j := i
while (j < size of s and bold[j] is non-zero), do −
(increase j by 1)
ret := substring of ret from index i to j - i - 1 concatenate "<b>" concatenate s
return ret
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: string addBoldTag(string s, vector<string>& dict) { int n = s.size(); vector<int> bold(n); string ret = ""; for (int i = 0, end = 0; i < s.size(); i++) { for (int j = 0; j < dict.size(); j++) { if (s.substr(i, dict[j].size()) == dict[j]) { end = max(end, i + (int)dict[j].size()); } } bold[i] = end > i; } int j; for (int i = 0; i < s.size(); i = j) { if (!bold[i]) { ret += s[i]; j = i + 1; continue; } j = i; while (j < s.size() && bold[j]) j++; ret += "<b>" + s.substr(i, j - i) + "</b>"; } return ret; } }; main(){ Solution ob; vector<string> v = {"abc","123"}; cout << (ob.addBoldTag("abcxyz123", v)); }
Input
"abcxyz123", ["abc","123"]
Output
<b>abc</b>xyz<b>123</b>
- Related Articles
- Program to enclose pattern into bold tag in Python?\n
- How do we add bold text in HTML?
- How to add bold annotated text in Matplotlib?
- How to add Tag Input in ReactJS?
- How to add a title in anchor tag using jQuery?
- How to Bold a Part of a Text String in a Cell in Excel?
- How to use Bold & Non-Bold Text In A Single UILabel in iOS/iPhone?
- How to add readonly attribute to an input tag in JavaScript?
- Adding paragraph tag to substrings within a string in JavaScript
- How to find anchor tag in div and add class using jQuery?
- How to bold text in checkbox in Excel?
- How to make text bold in HTML?
- How to add the tag of Azure VM using PowerShell?
- HTML DOM Bold object
- String Formatting in C# to add padding
