The Algorithm to Make Words Bold in HTML

  • 时间:2020-09-24 11:41:27
  • 分类:网络文摘
  • 阅读:131 次

Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any letters between and tags become bold.

The returned string should use the least number of tags possible, and of course the tags should form a valid combination.

For example, given that words = [“ab”, “bc”] and S = “aabcd”, we should return “aabcd”. Note that returning “aabcd” would use more tags, so it is incorrect.

Note:

  • words has length in range [0, 50].
  • words[i] has length in range [1, 10].
  • S has length in range [0, 500].
  • All characters in words[i] and S are lowercase letters.

Make String Bold using Bruteforce Algorithm

Without the requirement of the shortest, we can make bold on every occurrences of the words in the list. As we are given the original HTML string, we can mark bold for each character that appears in the bold words.

So, with O(N) space, and O(NM) where N is the size of the string and M is the total length of the bold string, the following C++ bruteforce algorithm will insert the least HTML bold tags that satisfy the requirement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
    string boldWords(vector<string>& words, string S) {
        int n = S.size();
        vector<bool> bold(n, false);
        for (const auto &s: words) {
            for (int i = 0; i + s.size() - 1 < n; ++ i) {
                bool ok = true;
                for (int k = 0; k < s.size(); ++ k) {
                    if (s[k] != S[i + k]) { // bold string s not found in the string
                        ok = false;
                        break;
                    }
                }
                if (ok) { // it is bold
                    for (int k = 0; k < s.size(); ++ k) {
                        bold[i + k] = true; // mark the character bold
                    }
                }
            }
        }
        string ans = "";
        for (int i = 0; i < n; ++ i) {
            // bold start boundary
            if (bold[i] && (i == 0 || !bold[i - 1])) ans += "<b>"; 
            ans += S[i];
            // bold end boundary
            if (bold[i] && (i == n - 1 || !bold[i + 1])) ans += "</b>";
        }
        return ans;
    }
};
class Solution {
public:
    string boldWords(vector<string>& words, string S) {
        int n = S.size();
        vector<bool> bold(n, false);
        for (const auto &s: words) {
            for (int i = 0; i + s.size() - 1 < n; ++ i) {
                bool ok = true;
                for (int k = 0; k < s.size(); ++ k) {
                    if (s[k] != S[i + k]) { // bold string s not found in the string
                        ok = false;
                        break;
                    }
                }
                if (ok) { // it is bold
                    for (int k = 0; k < s.size(); ++ k) {
                        bold[i + k] = true; // mark the character bold
                    }
                }
            }
        }
        string ans = "";
        for (int i = 0; i < n; ++ i) {
            // bold start boundary
            if (bold[i] && (i == 0 || !bold[i - 1])) ans += "<b>"; 
            ans += S[i];
            // bold end boundary
            if (bold[i] && (i == n - 1 || !bold[i + 1])) ans += "</b>";
        }
        return ans;
    }
};

After the bold characters are marked, we re-scan the string and look for the boundarys, and insert the tags accordingly.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
数学题:玉龙粮食加工厂生产一批面粉  数学题:从甲地到乙地的公路,只有上坡路和下坡路  数学题:求MN的长是多少  数学题:小红上学时坐车,回家步行  数学题:某班在植树活动中  数学题:三只猴子吃篮子里的桃子  数学题-实际比计划增加25%  甲植树的棵数是另外两人的二分之一  甲容器水深比乙容器水深低6厘米  第一次剪下2/5,又接上18米 
评论列表
添加评论