【LeetCode】616. Add Bold Tag in String 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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.
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:
- The given dict won’t contain duplicates, and its length won’t exceed 100.
- All the strings in input have length in range [1, 1000].
题目大意
给一个字符串 s
和一个字符串列表 dict
,你需要将在字符串列表中出现过的 s
的子串添加加粗闭合标签 <b>
和 </b>
。如果两个子串有重叠部分,你需要把它们一起用一个闭合标签包围起来。同理,如果两个子字符串连续被加粗,那么你也需要把它们合起来用一个加粗标签包围。
解题方法
遍历
这个题和758. Bold Words in String题是一模一样的题目。
先使用一个数组isBold保存S中的每个字符是否应该加粗,判断的方式暴力解决即可,即查找每个子串是否在words中出现过。
是否增加标签<b>
的方法是当前字符需要加粗,但是其前面的字符不用加粗,或者当前字符是第一个字符。
是否增加标签</b>
的方法是当前字符需要加粗,但是其后面的字符不用加粗,或者当前字符是最后一个字符。
C++代码如下:
class Solution {
public:
string addBoldTag(string s, vector<string>& dict) {
const int N = s.size();
vector<bool> isBold(N, false);
unordered_set<string> wordset(dict.begin(), dict.end());
for (string& word : dict) {
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 日 —— 今日又是九一八
【LeetCode】616. Add Bold Tag in String 解题报告(C++)的更多相关文章
- LeetCode 616. Add Bold Tag in String
原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...
- 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 ...
- 【LeetCode】758. Bold Words in String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [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 ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
随机推荐
- python11文件读写模块
将文件的打开和关闭,交给上下文管理工具with去实现. def read_file(): """ 读取文件 :return: """ fil ...
- Linux-设置终端界面的字体颜色和自定义常用快捷功能
.bashrc是一个隐藏的文件,要打开并修改该文件需要: (0)命令:cd ~ (1)命令:ls -a 找到文件 .bashrc: (2) 命令 vim ~/.bashrc 进入到文件: (3) 直接 ...
- Python队列queue模块
Python中queue模块常用来处理队列相关问题 队列常用于生产者消费者模型,主要功能为提高效率和程序解耦 1. queue模块的基本使用和相关说明 # -*- coding:utf-8 -*- # ...
- Google服务器架构图解简析
无疑是互联网时代最闪亮的明星.截止到今天为止,Google美国主站在Alexa排名已经连续3年第一,Alexa Top100中,各国的Google分站竟然霸占了超过20多个名额,不得不令人感叹Goog ...
- 【NetCore】RabbitMQ 封装
RabbitMQ 封装 代码 https://gitee.com/wosperry/wosperry-rabbit-mqtest/tree/master 参考Abp事件总线的用法,对拷贝的Demo进行 ...
- 在 windows 系统上 安装与配置 PHP + Apache
参考:http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html 在大学时候上过一门PHP课时,因为课堂需要配置过一次PHP+Mysql ...
- 转Android service 启动篇之 startForegroundService
本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...
- GO瞬间并发数控制
var wg2 sync.WaitGroup wg2.Add(nums) xc :=0 parallelNum := plt.MaxParallel var waitCount int32 = 0 f ...
- 搭建内网Yum源
搭建内网yum源 阅读(2,238) 一:因内网服务器 众多,当统一安装一些比较大的rpm的时候全部从外网下载就比较慢,而且还占用了一定的出口流量,因此在内网部署了一台yum服务器,将阿里云的epel ...
- 【Spring Framework】Spring入门教程(六)Spring AOP使用
Spring的AOP 动态代理模式的缺陷是: 实现类必须要实现接口 -JDK动态代理 无法通过规则制定拦截无需功能增强的方法. Spring-AOP主要弥补了第二个不足,通过规则设置来拦截方法,并对方 ...