[LeetCode] Add Bold Tag in String 字符串中增添加粗標籤

 

Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. Also, if two substrings wrapped by bold tags are consecutive, you need to combine them.html

Example 1:java

Input: 
s = "abcxyz123"
dict = ["abc","123"]
Output:
"<b>abc</b>xyz<b>123</b>"

 

Example 2:數組

Input: 
s = "aaabbcc"
dict = ["aaa","aab","bc"]
Output:
"<b>aaabbc</b>c"

 

Note:app

  1. The given dict won't contain duplicates, and its length won't exceed 100.
  2. All the strings in input have length in range [1, 1000].

 

這道題給咱們了一個字符串,還有一個字典,讓咱們把字符串中在字典中的單詞加粗,注意若是兩個單詞有交集或者相接,就放到同一個加粗標籤中。博主剛開始的想法是,既然須要匹配字符串,那麼就上KMP大法,而後獲得每一個單詞在字符串匹配的區間位置,而後再合併區間,再在合併後的區間兩頭加標籤。可是一看題目難度,Medium,中等難度的題不至於要祭出KMP大法吧,因而去網上掃了一眼衆神們的解法,發現大多都是暴力匹配啊,既然OJ能過去,那麼就一塊兒暴力吧。這題參考的是高神shawngao的解法,高神但是集了一千五百多個讚的男人,叼到飛起!思路是建一個和字符串s等長的bold布爾型數組,表示若是該字符在單詞裏面就爲true,那麼最後咱們就能夠根據bold數組的真假值來添加標籤了。咱們遍歷字符串s中的每個字符,把遍歷到的每個字符看成起始位置,咱們都匹配一遍字典中的全部單詞,若是能匹配上,咱們就用i + len來更新end,len是當前單詞的長度,end表示字典中的單詞在字符串s中結束的位置,那麼若是i小於end,bold[i]就要賦值爲true了。最後咱們更新完bold數組了,就再遍歷一遍字符串s,若是bold[i]爲false,直接將s[i]加入結果res中;若是bold[i]爲true,那麼咱們用while循環來找出全部連續爲true的個數,而後在左右兩端加上標籤,參見代碼以下:post

 

解法一:ui

class Solution {
public:
    string addBoldTag(string s, vector<string>& dict) {
        string res = "";
        int n = s.size(), end = 0;
        vector<bool> bold(n, false);
        for (int i = 0; i < n; ++i) {
            for (string word : dict) {
                int len = word.size();
                if (i + len <= n && s.substr(i, len) == word) {
                    end = max(end, i + len);
                }
            }
            bold[i] = end > i;
        }
        for (int i = 0; i < n; ++i) {
            if (!bold[i]) {
                res.push_back(s[i]);
                continue;
            }
            int j = i;
            while (j < n && bold[j]) ++j;
            res += "<b>" + s.substr(i, j - i) + "</b>";
            i = j - 1;
        }
        return res;
    }
};

 

這道題跟以後的那道Bold Words in String是如出一轍的題,那麼解法固然是能夠互通的了,這裏咱們把那道題中解法二也貼過來吧,因爲解法一和解法二實在是太類似了,就貼一個吧,具體講解能夠參見Bold Words in String這篇帖子,參見代碼以下:url

 

解法二:spa

class Solution {
public:
    string addBoldTag(string s, vector<string>& dict) {
        string res = "";
        int n = s.size();
        unordered_set<int> bold;
        for (string word : dict) {
            int len = word.size();
            for (int i = 0; i <= n - len; ++i) {
                if (s[i] == word[0] && s.substr(i, len) == word) {
                    for (int j = i; j < i + len; ++j) bold.insert(j);
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            if (bold.count(i) && !bold.count(i - 1)) res += "<b>";
            res += s[i];
            if (bold.count(i) && !bold.count(i + 1)) res += "</b>";
        }
        return res;
    }
};

 

相似題目:code

Tag Validatorhtm

Merge Intervals

Bold Words in String

 

參考資料:

https://discuss.leetcode.com/topic/92112/java-solution-boolean-array

 

LeetCode All in One 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索