-
Notifications
You must be signed in to change notification settings - Fork 115
/
0758-BoldWordsInString.cs
43 lines (38 loc) · 1.24 KB
/
0758-BoldWordsInString.cs
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//-----------------------------------------------------------------------------
// Runtime: 108ms
// Memory Usage: 24.9 MB
// Link: https://leetcode.com/submissions/detail/352381504/
//-----------------------------------------------------------------------------
using System.Text;
namespace LeetCode
{
public class _0758_BoldWordsInString
{
public string BoldWords(string[] words, string S)
{
var bold = new bool[S.Length + 1];
foreach (var word in words)
{
var startIndex = S.IndexOf(word, 0);
while (startIndex >= 0)
{
for (int i = 0; i < word.Length; i++)
bold[i + startIndex] = true;
startIndex = S.IndexOf(word, startIndex + 1);
}
}
var sb = new StringBuilder();
if (bold[0])
sb.Append("<b>");
for (int i = 0; i < S.Length; i++)
{
sb.Append(S[i]);
if (!bold[i] && bold[i + 1])
sb.Append("<b>");
if (bold[i] && !bold[i + 1])
sb.Append("</b>");
}
return sb.ToString();
}
}
}