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. Springmvc 视频学习地址

    http://www.icoolxue.com/album/show/216/

  2. echarts图表变形解决方案

    在同一页面的多个echarts图在查询或切换图片时可能会变形,如图 解决方案是添加以下几行代码 /*在查询或切换统计图时图片有可能会变形,于是每次调getEchartsData()都给每个chart的 ...

  3. 【Java实现】栈和队列就是这么简单

    一.前言 上一篇已经讲过了链表[Java实现单向链表]了,它跟数组都是线性结构的基础,本文主要讲解线性结构的应用:栈和队列 如果写错的地方希望大家能够多多体谅并指正哦,如果有更好的理解的方式也希望能够 ...

  4. ASP.NET没有魔法——ASP.NET MVC Razor与View渲染

    对于Web应用来说,它的界面是由浏览器根据HTML代码及其引用的相关资源进行渲染后展示给用户的结果,换句话说Web应用的界面呈现工作是由浏览器完成的,Web应用的原理是通过Http协议从服务器上获取到 ...

  5. 关于SELECT LAST_INSERT_ID()的使用规则

    尊重个人劳动成果,转载请注明出处: http://blog.csdn.net/czd3355/article/details/71302441 首先我先解释以下在在映射文件中的代码是什么意思. < ...

  6. Java基础学习笔记七 Java基础语法之继承和抽象类

    继承 继承的概念 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继承可以使多种事物之间形成一种关系体系. 例如公司中的研发部员工和维护部员工都属于员工, ...

  7. 第二次作业评分可能要 delay 一些

    各位同学,因为我现在在出差,昨天刚刚到旧金山,加上倒时差,所以这次作业我处理得会更慢一些,希望谅解. 另外,博客园的邮件通知邮件好多都进垃圾箱了,所以如果你有什么问题我没回且你关心的,请给我写邮件:j ...

  8. alpha-咸鱼冲刺day3-紫仪

    总汇链接 一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 今天把数据库的表给建好了,学长那边把登陆跟注册页面也做好了(纯页面,html5+css的那种) 四,问题困难   日常 ...

  9. 201621123050 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...

  10. java的socket通信

    本文讲解如何用java实现网络通信,是一个非常简单的例子,我比较喜欢能够立马看到结果,所以先上代码再讲解具体细节. 服务端: import java.io.BufferedReader; import ...