- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址: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:
1、 Thegivendictwon'tcontainduplicates,anditslengthwon'texceed100.;
2、 Allthestringsininputhavelengthinrange[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;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
2022
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
我一直在处理我的 richtextbox 但我遇到了一些奇怪的事情......我想把每一行的第一个词加粗 使用此代码: RichTextBox bold = richTextBox1;
我在网上寻找加粗 NSString 的方法,但它要么不存在,要么我似乎找不到。我只是想在 NSString 中加粗这个词,但到目前为止,我所做的还没有实现: NSString *player1Nam
我有一个 DataGridView 并且想要将 Header 文本设为粗体。我尝试将 ColumnHeaderDefaultCellStyle 更改为 DataGridViewCellStyle {
是否可以在 UIButton 中加粗字体?使用标签,它非常简单,例如: label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0) 但这不适用
是否可以将 NSString 设为粗体?如果是这样,怎么办?由于某种原因,我没有使用 UILabel 或 UIIextView。 最佳答案 NSString 保存字符串,但没有文本格式。我假设你可以使
我正在为我们的组织开发一个上传门户,当人们向我们的编码提交文件时,我希望它生成的 URL 为粗体。我对 HTML 和编码有基本的了解,但在尝试将生成的 URL (+file.getURL()) 粗体化
我在列表中显示一些内容以在 pdf 文件中显示它。 一切正常,但现在我希望列表项中的一些文本应该是粗体。 例如: 这是 ListItem 粗体 文本。 我该怎么做? 这是我的代码: List lst
我在一页上有一堆如下所示的 div。有没有办法在 css 中加粗第一行(在 之前)?因此,在这种情况下,我希望客户以粗体显示。 Customer John Doe 最佳答案 是的,您正在寻找:
$need_it_sooner = l( t('Need it sooner? Click here'), 'need-it-sooner', array( 'attribut
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Is there a way to style part of an input field’s value
我有一个 jquery 可以像这样更改链接的文本: if (urlfind > 0) { $('#linkurl').text('More info'); } 和 html: 我正在尝试为此
我正在尝试制作一个简单的文本编辑器,以便用户能够加粗/取消加粗所选文本。我想使用 Window.getSelection() 而不是 Document.execCommand()。它完全符合我的要求,
我正在尝试制作一个简单的文本编辑器,以便用户能够加粗/取消加粗所选文本。我想使用 Window.getSelection() 而不是 Document.execCommand()。它完全符合我的要求,
标题说明了一切。我想要实现的是检查菜单中的 onload url(多个 ul,因为它也有子菜单),如果它等于,则将其设为粗体并触发鼠标悬停事件: $(document).ready(function(
我正在创建一些 HTML/CSS 来显示一个键,但如果标题文本与元素文本相比,最终会出现奇怪的垂直错位。 经过大量试验,我发现问题似乎只发生在文本加粗时,而且只有在使用 Lucida Sans 字体时
我注意到,当我将 alpha 参数添加到 geom 时,当输出为 PDF 时,图表上的所有文本(轴标题等)都会变暗(几乎就像它们变得粗体一样)。这似乎与实际的 alpha 值无关。它使图表更难看,特别
我知道当属性在属性页中显示为粗体时,表示它已被修改。 https://msdn.microsoft.com/en-us/library/675f1588.aspx 我有一个 API,我们随附样本,我希
我有以下代码,我需要将 profile.userId 设为粗体: {{'intranet.profile.dashboard.lastLoggedIn' | messageBundle: p
我是一名优秀的程序员,十分优秀!