题目地址:https://leetcode-cn.com/problems/bold-words-in-string/数组
Given a set of keywords words and a string S
, make all appearances of all keywords in S
bold. Any letters between <b>
and </b>
tags become bold.app
The returned string should use the least number of tags possible, and of course the tags should form a valid combination.ide
For example, given that words = ["ab", "bc"]
and S = "aabcd"
, we should return "a<b>abc</b>d"
. Note that returning "a<b>a<b>b</b>c</b>d"
would use more tags, so it is incorrect.code
Note:orm
words
has length in range [0, 50]
.words[i]
has length in range [1, 10]
.S
has length in range [0, 500]
.words[i]
and S
are lowercase letters.给定一个关键词集合 words 和一个字符串 S,将全部 S 中出现的关键词加粗。全部在标签 <b>
和 </b>
中的字母都会加粗。
返回的字符串须要使用尽量少的标签,固然标签应造成有效的组合。
例如,给定 words = ["ab", "bc"]
和 S = "aabcd"
,须要返回 "a<b>abc</b>d"
。注意返回 "a<b>a<b>b</b>c</b>d"
会使用更多的标签,所以是错误的。leetcode
先使用一个数组isBold保存S中的每一个字符是否应该加粗,判断的方式是,遍历words中的每一个字符串,找出S中有哪些位置和它匹配。字符串
是否增长标签<b>
的方法是当前字符须要加粗,可是其前面的字符不用加粗,或者当前字符是第一个字符。
是否增长标签</b>
的方法是当前字符须要加粗,可是其后面的字符不用加粗,或者当前字符是最后一个字符。string
C++代码以下:it
class Solution { public: string boldWords(vector<string>& words, string S) { const int N = S.size(); vector<bool> isBold(N, false); for (string& word : words) { for (int i = 0; i < N; ++i) { string sub = S.substr(i, word.size()); if (sub == word) { for (int k = i; k < i + word.size(); ++k) { isBold[k] = true; } } } } string res; for (int i = 0; i < N; ++i) { if (isBold[i] && (i == 0 || !isBold[i - 1])) { res += "<b>"; } res += S[i]; if (isBold[i] && (i == N - 1 || !isBold[i + 1])) { res += "</b>"; } } return res; } };
2019 年 9 月 18 日 —— 今日又是九一八io