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.

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 "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.

Note:

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

这道题跟之前的那道Add Bold Tag in String是一模一样的,之前还换个马甲,这次连场景都不换了,直接照搬啊?!我也是服气的~这道题应该没有太多的技巧,就是照题目意思来就行了,我们使用一个数组bold,标记所有需要加粗的位置为true,初始化所有为false。我们首先要判断每个单词word是否是S的子串,判断的方法就是逐个字符比较,遍历字符串S,找到和word首字符相等的位置,并且比较随后和word等长的子串,如果完全相同,则将子串所有的位置在bold上比较为true。等我们知道了所有需要加粗的位置后,我们就可以来生成结果res了,我们遍历bold数组,如果当前位置是true的话,表示需要加粗,那么我们首先看如果是第一个字符,或者其前面的字符不用加粗,我们加上一个左标签<b>,然后我们将当前字符加入结果res中,然后再判断,如果当前是末尾字符,或者后面一个字符不用加粗,则需要加上一个右标签</b>;如果当前位置是false,我们直接将字符加入结果res中即可,参见代码如下:

解法一:

class Solution {
public:
string boldWords(vector<string>& words, string S) {
int n = S.size();
string res = "";
vector<bool> bold(n, false);
for (string word : words) {
int len = word.size();
for (int i = ; i <= n - len; ++i) {
if (S[i] == word[] && S.substr(i, len) == word) {
for (int j = i; j < i + len; ++j) bold[j] = true;
}
}
}
for (int i = ; i < n; ++i) {
if (bold[i]) {
if (i == || !bold[i - ]) res += "<b>";
res.push_back(S[i]);
if (i == n - || !bold[i + ]) res += "</b>";
} else {
res.push_back(S[i]);
}
}
return res;
}
};

我们可以用HashSet来代替数组,只是将需要加粗的位置放入HashSet,然后我们在生成结果res的时候,先检测当前位置是否加粗,如果加粗了,并且前一个位置不在HashSet中,这样就不用判断是否是第一个元素了,因为i-1肯定不再HashSet中,也不像数组那样存在越界的可能,我们给结果res加上左标签,然后将当前的字符加入结果res中,然后再判断如果当前位置如果加粗了,并且下一个位置不在HashSet中,我们给结果res加上右标签,参见代码如下:

解法二:

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

前面提到了这道题跟Add Bold Tag in String是完全一样,那么当然二者的解法是互通的,下面的解法是之前那道题中的解法,其实整体思路是一样的,只不过在构建的bold数组的时候,是先遍历的字符串S,而不是先遍历的单词。对于字符串S中的每个字符为起点,我们都遍历下所有单词,如果某个单词是以当前字符为起点的子串的话,那么我们用i+len来更新end,所以遍历完所有单词后,只要当前位置需要加粗,那么end一定大于i,通过这种方法同样也可以生成正确的bold数组。然后在创建结果res字符串的时候也跟上面的方法有些不同,首先判断,如果当前未被加粗,那么将当前字符存入结果res中并且continue,否则开始找相连的需要加粗的位置,用j来指向下一个不用加粗的位置,这样中间的子串就可以放入标签中整体加到res中,然后继续在后面查找连续加粗的子串,参见代码如下:

解法三:

class Solution {
public:
string boldWords(vector<string>& words, string S) {
int n = S.size(), end = ;
string res = "";
vector<bool> bold(n, false);
for (int i = ; i < n; ++i) {
for (string word : words) {
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 = ; 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 - ;
}
return res;
}
};

类似题目:

Add Bold Tag in String

参考资料:

https://leetcode.com/problems/bold-words-in-string/discuss/113107/Java-Solution-without-HashMap

https://leetcode.com/problems/bold-words-in-string/discuss/113093/Clean-Python-and-C++-solutions-with-hashset-to-store-bold-indices

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Bold Words in String 字符串中的加粗单词的更多相关文章

  1. [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 ...

  2. [LeetCode] 567. Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  3. String 字符串中含有 Unicode 编码时,转为UTF-8

    1.单纯的Unicode 转码 String a = "\u53ef\u4ee5\u6ce8\u518c"; a = new String(a.getBytes("UTF ...

  4. 将string字符串中的换行符进行替换

    /** * 方法名称:replaceBlank * 方法描述: 将string字符串中的换行符进行替换为"" * */ public static String replaceBl ...

  5. JS返回一个字符串中长度最小的单词的长度

    题目:编写一个方法,返回字符串中最小长度的单词的长度. var str = 'What a good day today!'; 1 //方法一 2 function returnString1(str ...

  6. [LeetCode] Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  7. 434 Number of Segments in a String 字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  8. [CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

    1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...

  9. String:(字符串)中常用的方法

    package stringyiwen; //字符串中常用的方法public class StringTest03 { public static void main(String[] args) { ...

随机推荐

  1. Spring Framework 简介

    Spring Framework 依赖注入.事务管理.Web应用程序.数据访问.消息传递.测试和更多的核心支持. Tips: Spring 官网:https://spring.io/ spring f ...

  2. Android,资料分享(2015 版)

    Java 学习 我要再次强调,一定要有Java 基础(虽然现在使用其他语言也可以开发Android,但毕竟是很小众),也不要认为学习Java 两三周就可以不用管了,这会在以后的深入学习中暴露出问题,所 ...

  3. HashMap的底层原理

    简单说: 底层原理就是采用数组加链表: 两张图片很清晰地表明存储结构: 既然是线性数组,为什么能随机存取?这里HashMap用了一个小算法,大致是这样实现: // 存储时: int hash = ke ...

  4. [福大软工] W班 评测作业对应表

  5. Alpha第十天

    Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  6. 敏捷冲刺每日报告——Day4

    1.情况简述 Alpha阶段第一次Scrum Meeting 敏捷开发起止时间 2017.10.28 00:00 -- 2017.10.29 00:00 讨论时间地点 2017.10.28晚9:30, ...

  7. 结对编程作业——四则运算GUI程序

    毛忠庆 201421122088 赵嘉楠 201421122065 源代码存放位置:https://gitee.com/ouwen0819/SiZeYunSuan.git 题目描述 使用 -n 参数控 ...

  8. Django 模型层

    基本操作 1.meta类属性汇总 属性名 用法 举例代码 abstract 如果设置abstract=True则这个模式是一个抽象基类   db_table 定义model在数据库中的表名称,如果不定 ...

  9. 构建微服务开发环境3————Java应用的优秀管理工具Maven的下载安装及配置

    [内容指引] 下载安装包: MacOS下Maven的安装及配置: Windows下Maven的安装及配置. 一.下载安装包 进入Maven的官方下载地址:http://maven.apache.org ...

  10. HP DL380服务器RAID信息丢失数据恢复方法和数据恢复过程分享

    [数据恢复故障描述]    客户服务器属于HP品牌DL380系列,存储是由6块73GB SAS硬盘组成的RAID5,操作系统是WINDOWS 2003 SERVER,主要作为企业部门内部的文件服务器来 ...