[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:

  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].

s

时间: 2024-10-16 14:47:57

[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 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 fo

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

C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串,它的一种原型为: string& insert (size_t pos, const string& str); pos 表示要插入的位置,也就是下标:str 表示要插入的字符串,它可以是 string 变量,也可以是C风格的字符串. 请看下面的代码: #include <iostrea

三. Anagram detection problem for string(字符串中回文词汇检测问题)

anagram 相同字母异序词.heart vs earth 1.Our first solution to the anagram problem will check to see that each character in the first string actually occurs in the second. If it is possible to "checkoff" each character, then the two strings must be anag

[CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure? 这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用.像普通的整型数组应该还是能用的,这道题的小技巧就是用整型数组来代替哈希表,在之前Bitwise AND of Numbers Range 数

【easy】Number of Segments in a String 字符串中的分段数量

以空格为分隔符,判断一个string可以被分成几部分. 注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格 思路: 只要统计出单词的数量即可.那么我们的做法是遍历字符串,遇到空格直接跳过,如果不是空格,则计数器加1,然后用个while循环找到下一个空格的位置,这样就遍历完了一个单词,再重复上面的操作直至结束,就能得到正确结果: class Solution { public: int countSegments(string s) { int res = 0, n = s.size(

【leetcode 简单】 第九十题 字符串中的第一个唯一字符

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ s_len=len(s) for i in &qu

C# 正则表达式匹配string字符串中的时间串(yyyyMMdd)

var time = ""; string pattern = @"_(?<time>20\d{6})"; var regResult = Regex.Match(fileName, pattern); if (regResult.Success) { time = regResult.Groups["time"].ToString(); } var uploadDate = DateTime.ParseExact(time, &qu

正则去除html字符串中的注释、标签、属性

var str = '<!-- 注释1 --><h1 style="color:#00ff00;text-align: center;">ProsperLee<!-- 注释 --></h1>'; document.write(str.replace(/<!--[\w\W\r\n]*?-->/gmi, '')); // 去除HTML中的注释 document.write(str.replace(/<[^>]+>

345. 反转字符串中元音字母的位置 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede" 题意:反转字符串中元音字母的位置 方法1:用栈保存元音字符串,时间