-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
42 lines (41 loc) · 1.26 KB
/
Program.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
using System;
namespace BoldWordsInString
{
class Program
{
static void Main(string[] args)
{
string[] words = {"ab", "bc"};
string S = "aabcd";
Console.WriteLine(BoldWords(words, S));
}
static string BoldWords(string[] words, string S)
{
var isBold = new bool[S.Length + 2];
var letters = new string[S.Length];
for (int i = 0; i < S.Length; i++)
{
letters[i] = S[i].ToString();
foreach (var word in words)
{
if(S.Length - i < word.Length) continue;
if (S.Substring(i, word.Length) == word)
{
for (int j = 0; j < word.Length; j++)
isBold[i + j + 1] = true;
}
}
}
var res = "";
for (int i = 0; i < letters.Length; i++)
{
if (isBold[i + 1] && !isBold[i])
letters[i] = "<b>" + letters[i];
if (isBold[i + 1] && !isBold[i + 2])
letters[i] += "</b>";
res += letters[i];
}
return res;
}
}
}