题目地址: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.

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.

题目大意

给定一个关键词集合 words 和一个字符串 S,将所有 S 中出现的关键词加粗。所有在标签 <b></b> 中的字母都会加粗。
返回的字符串需要使用尽可能少的标签,当然标签应形成有效的组合。
例如,给定 words = ["ab", "bc"]S = "aabcd",需要返回 "a<b>abc</b>d"。注意返回 "a<b>a<b>b</b>c</b>d" 会使用更多的标签,因此是错误的。

解题方法

遍历

先使用一个数组isBold保存S中的每个字符是否应该加粗,判断的方式是,遍历words中的每个字符串,找出S中有哪些位置和它匹配。

是否增加标签<b>的方法是当前字符需要加粗,但是其前面的字符不用加粗,或者当前字符是第一个字符。
是否增加标签</b>的方法是当前字符需要加粗,但是其后面的字符不用加粗,或者当前字符是最后一个字符。

C++代码如下:

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 日 —— 今日又是九一八

【LeetCode】758. Bold Words in String 解题报告(C++)的更多相关文章

  1. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

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

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

  3. 【LeetCode】761. Special Binary String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...

  4. 【LeetCode】567. Permutation in String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...

  5. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  6. LeetCode: Reverse Words in a String 解题报告

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

  7. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  8. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  9. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. python11文件读写模块

    将文件的打开和关闭,交给上下文管理工具with去实现. def read_file(): """ 读取文件 :return: """ fil ...

  2. Go 性能提升tips--边界检查

    1. 什么是边界检查? 边界检查,英文名 Bounds Check Elimination,简称为 BCE.它是 Go 语言中防止数组.切片越界而导致内存不安全的检查手段.如果检查下标已经越界了,就会 ...

  3. 面向Web应用的并发压力测试工具——Locust实用攻略

    1. 概述 该方案写作目的在于描述一个基于Locust实现的压力测试,文中详细地描述了如何利用locustfile.py文件定义期望达成的测试用例,并利用Locust对目标站点进行并发压力测试. 特别 ...

  4. day11 函数

    day11 函数 一.函数基础 """ 1 什么是函数 函数是盛放代码的容器:把实现某一功能的代码放到一个函数内就制造一个工具 2 为何要用函数 没有用函数之前程序的问题 ...

  5. 【swift】Xcode未响应(卡死、卡住、CPU满载、忙碌、转圈圈)

    在尝试了网上的方法,依然没能解决问题,尝试如下: 1.去自己项目的路径,找到<你的项目名.xcodeproj>,点击[显示包内容],删除xcuserdata文件夹 2.去Library,把 ...

  6. android 获取uri的正确文件路径的办法

    private String getRealPath( Uri fileUrl ) { String fileName = null; if( fileUrl != null ) { if( file ...

  7. 什么是javaScript闭包

    闭包是与函数有着紧密的关系,它是函数的代码在运行过程中的一个动态环境,是一个运行期的概念. 所谓闭包,是指词法表示包括不必计算的变量的函数.也就是说,该函数能够使用函数外定义的变量. 在程序语言中,所 ...

  8. 【Linux】【Shell】【text】awk

    基本用法:gawk [options] 'program' FILE ...             program: PATTERN{ACTION STATEMENTS}               ...

  9. TCP协议三步挥手与四步挥手

    关于TCP协议 TCP(Transmission Control Protocol, 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.与之对应的是UDP(User Datagram ...

  10. Python——连接数据库操作

    一.数据库基础用法 要先配置环境变量,然后cmd安装:pip install pymysql 1.连接MySQL,并创建wzg库 #引入decimal模块 import pymysql #连接数据库 ...