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.

Example 1:

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:

  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的个数,然后在左右两端加上标签,参见代码如下:

解法一:

class Solution {
public:
string addBoldTag(string s, vector<string>& dict) {
string res = "";
int n = s.size(), end = ;
vector<bool> bold(n, false);
for (int i = ; 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 = ; 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;
}
};

这道题跟之后的那道Bold Words in String是一模一样的题,那么解法当然是可以互通的了,这里我们把那道题中解法二也贴过来吧,由于解法一和解法二实在是太相似了,就贴一个吧,具体讲解可以参见Bold Words in String这篇帖子,参见代码如下:

解法二:

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 = ; 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 += s[i];
if (bold.count(i) && !bold.count(i + )) res += "</b>";
}
return res;
}
};

类似题目:

Tag Validator

Merge Intervals

Bold Words in String

参考资料:

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

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

[LeetCode] Add Bold Tag in String 字符串中增添加粗标签的更多相关文章

  1. [LeetCode] Bold Words in String 字符串中的加粗单词

    Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any le ...

  2. LeetCode 616. Add Bold Tag in String

    原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...

  3. 【LeetCode】616. Add Bold Tag in String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  4. 616. 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&g ...

  5. LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium

    题目:Reverse Words in a String Given an input string, reverse the string word by word. For example, Gi ...

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

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

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

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

  8. C++学习38 string字符串的增删改查

    C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串, ...

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

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

随机推荐

  1. linux小白成长之路3————更新yum源

    [内容指引] 进入目录:cd 查看目录下的内容:ls 重命名备份:mv 从网络下载:wget yum更新:yum update 第一次运行yum安装软件前,建议更新yum. 1.进入yum源目录 命令 ...

  2. 如何从零开始学习区块链技术——推荐从以太坊开发DApp开始

    很多人迷惑于区块链和以太坊,不知如何学习,本文简单说了一下学习的一些方法和资源. 一. 以太坊和区块链的关系 从区块链历史上来说,先诞生了比特币,当时并没有区块链这个技术和名词,然后业界从比特币中提取 ...

  3. 小米官网的css3导航菜单

    HTML代码: <div class="nav"> <ul> <li><a href="#">首页</a& ...

  4. java中的异常以及 try catch finally以及finally的执行顺序

    java中的 try.catch.finally及finally执行顺序详解: 1.首相简单介绍一下异常以及异常的运行情况: 在Java中异常的继承主要有两个: Error和Exception 这两个 ...

  5. RTMP消息详细介绍

    本文继上篇简单分析了RTMP协议如何进行通信进一步详细分析RTMP的消息都有哪些,以及这些消息有什么作用. 一.RMTP消息 由上一篇文章可知RTMP 消息有分成两个部分,一个是头部,一个是有效负载. ...

  6. C语言程序设计(基础)- 第4周作业

    一.PTA作业 完成PTA第四周作业中8个题目,并将其中4个题目的思路列在博客中. 1.7-1 计算分段函数[1] 2.7-2 A除以B 3.7-6 阶梯电价 4.7-7 出租车计价 随笔具体书写内容 ...

  7. C语言指针作业

    一.PTA实验作业 题目1:6-5 判断回文字符串 1. 本题PTA提交列表 2. 设计思路 3.代码截图 4.本题调试过程碰到问题及PTA提交列表情况说明. 第一次做的时候我j直接等于count,其 ...

  8. Basic FIFO Queue

    Queue - 一种线程安全的FIFO实现 Python的Queue模块提供一种适用于多线程编程的FIFO实现.它可用于在生产者(producer)和消费者(consumer)之间线程安全(threa ...

  9. 项目Beta冲刺Day7

    项目进展 李明皇 今天解决的进度 部分数据传递和使用逻辑测试 林翔 今天解决的进度 服务器端查看个人发布的action,修改已发布消息状态的action,仍在尝试使用第三方云存储功能保存图片 孙敏铭 ...

  10. hadoop基础教程免费分享

    提起Hadoop相信大家还是很陌生的,但大数据呢?大数据可是红遍每一个角落,大数据的到来为我们社会带来三方面变革:思维变革.商业变革.管理变革,各行业将大数据纳入企业日常配置已成必然之势.阿里巴巴创办 ...