using System;
using System.Reflection;
using System.Text;
public class HelloWorld
{
public static void Main()
{
var text = ModifyString("000101101000110011001111100110111101000110110101110101011");
Console.WriteLine($"{text}");
}
private static string ModifyString(string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
return string.Join(
(string)input.Aggregate(new StringBuilder(), (sb, c) =>
{
switch (sb.Length)
{
case > 0 when sb[^1] == c:
break;
case > 0:
{
var lastChar = sb[^1];
var consecutiveCount = 0;
for (var i = sb.Length - 1; i >= 0 && sb[i] == lastChar; i--) {
consecutiveCount++;
}
if(consecutiveCount < 3) {
sb.Remove(sb.Length - consecutiveCount, consecutiveCount);
sb.Append('0', consecutiveCount);
}
break;
}
}
sb.Append(c);
return sb;
}).ToString().AsEnumerable(), "");
}
}