Skip to content

Instantly share code, notes, and snippets.

@dluciano
Created November 5, 2022 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dluciano/ad1327f3c21fb450a1784f091d00353d to your computer and use it in GitHub Desktop.
Save dluciano/ad1327f3c21fb450a1784f091d00353d to your computer and use it in GitHub Desktop.
616. Add Bold Tag in String
public class Solution {
public string AddBoldTag(string s, string[] words) {
BitArray FindMatches(){
var i = 0;
var bits = new BitArray(s.Length);
while(i < s.Length){
foreach(var word in words){
if(i + word.Length > s.Length)
continue;
var k = i;
for(var j = 0; j < word.Length; ++j, ++k){
if(word[j] != s[k])
break;
}
// i: 6, k: 8
//Console.WriteLine($"i: {i}, k: {k}, {word}, {k - i == word.Length}");
if(k - i == word.Length){
for(int j = i, l = 0; l < word.Length; ++j, ++l)
bits[j] = true;
}
}
i++;
}
return bits;
}
var bits = FindMatches();
var sb = new StringBuilder();
var i = 0;
// for(var k = 0; k < bits.Length; k++)
// Console.Write($"{(bits[k] ? 1 : 0)}");
while(i < bits.Length){
if(bits[i]){
sb.Append("<b>");
while(i < bits.Length && bits[i]){
sb.Append(s[i]);
i++;
}
sb.Append("</b>");
}
else{
sb.Append(s[i]);
i++;
}
}
return sb.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment