C++ 22ms solution, not fancy at all


  • 0
    C

    Find all the occurrences of all the dict items, and mark them bold. Then prepare the final string.

    class Solution {
    public:
        string addBoldTag(string s, vector<string>& dict) {
            vector<bool> bold (s.size(), false);
            for (auto & d : dict) {
                int i = s.find(d);
                while (i < s.size() && i != string::npos) {
                    for (int j = i; j < i + d.size(); j++) bold[j] = true;
                    i = s.find(d, i + 1);
                }
            }
            string res;
            bool pre = false;
            for (int i = 0; i < bold.size(); i++) {
                if (!bold[i] && pre) {
                    res += "</b>";
                    pre = false;
                }
                else if (bold[i] && !pre){
                    res += "<b>";
                    pre = true;
                }
                res += s[i];
            }
            if (pre) res += "</b>";
            
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.