[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 </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].
这道题给我们了一个字符串,还有一个字典,让我们把字符串中在字典中的单词加粗,注意如果两个单词有交集或者相接,就放到同一个加粗标签中。博主刚开始的想法是,既然需要匹配字符串,那么就上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;
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/92112/java-solution-boolean-array
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Add Bold Tag in String 字符串中增添加粗标签的更多相关文章
- [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 ...
- LeetCode 616. Add Bold Tag in String
原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...
- 【LeetCode】616. Add Bold Tag in String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 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: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 ...
- String 字符串中含有 Unicode 编码时,转为UTF-8
1.单纯的Unicode 转码 String a = "\u53ef\u4ee5\u6ce8\u518c"; a = new String(a.getBytes("UTF ...
- 将string字符串中的换行符进行替换
/** * 方法名称:replaceBlank * 方法描述: 将string字符串中的换行符进行替换为"" * */ public static String replaceBl ...
- C++学习38 string字符串的增删改查
C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串, ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
随机推荐
- java性能调优---------------------JVM调优方案
JVM的调优的主要过程有: 1.确定堆内存大小(-Xmx.-Xms) 2.合理分配新生代和老年代(-XX:NewRatio.-Xmn.-XX:SurvivorRatio) 3.确定永久区大小(-XX: ...
- CSS速查列表-1-(background)背景
CSS 背景 CSS 属性定义背景效果: background-color background-image background-repeat background-attachment backg ...
- 20162330 实验二 《Java面向对象程序设计》 实验报告
2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验二 <Java面向对象程序设计> 实验报告 课程名称:<程序设计与数据结构> 学生班级:162 ...
- 201621123060《JAVA程序设计》第一周学习总结
1.本周学习总结 1.讲述了JAVA的发展史,关于JDK.JRE.JVM的联系和区别 2.JDK是用JAVA开发工具.做项目的关键.JRE是JAVA的运行环境(JAVA也是JAVA语言开发的).JVM ...
- 20162328蔡文琛week05
学号 20162328 <程序设计与数据结构>第X周学习总结 教材学习内容总结 面向对象程序设计的核心是类的定义,它代表定义了状态和行为的对象. 变量的作用域依赖于变量声明的位置,作用域决 ...
- 20162323周楠《Java程序设计与数据结构》第五周总结
20162323周楠 2016-2017-2 <程序设计与数据结构>第五周学习总结 教材学习内容总结 1.面向对象软件设计的基本部分是确定程序中应该创建哪些类: 2.面向对象程序设计的核心 ...
- 利用python实现简单登陆注册系统
#!/usr/bin/env python # -*- coding:utf-8 -*- def login(username,password): ''' :param username:用户名 : ...
- Linux 帳號管理與 ACL 權限設定
1. Linux 的账号与群组1.1 使用者识别: UID 与 GID1.2 使用者账号:/etc/passwd, /etc/shadow1.3 关于群组: 有效与初始群组. groups, newg ...
- EXT3文件系统误删除导致文件系统中的邮件丢失恢复方法
一.故障描述 由8块盘组成的RAID5, 上层是EXT3文件系统,由于误删除导致文件系统中的邮件丢失 二.镜像磁盘为防止数据恢复过程中由于误操作对原始磁盘造成二次破坏, 使用winhex软件为每块磁盘 ...
- 03-移动端开发教程-CSS3新特性(下)
1. CSS3动画 1.1 过渡的缺点 transition的优点在于简单易用,但是它有几个很大的局限. transition需要事件触发,所以没法在网页加载时自动发生. transition是一次性 ...