using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//string input = "10001";
//string input = "00110";
string input = "01010";
int count = 0;
int currIndex = 0;
int start = 0;
char currType = input[0];
int typeChange = -1;
while (start < input.Length - 1)
{
if (currIndex > input.Length - 1 || input[currIndex] == currType)
{
if (typeChange != -1)
{
if (typeChange - start <= (currIndex - 1)- typeChange)
count += typeChange - start;
else
count += currIndex - typeChange;
currType = input[typeChange];
currIndex = typeChange + 1;
start = typeChange;
typeChange = -1;
continue;
}
}
else if (input[currIndex] != currType)
{
if (typeChange == -1)
{
typeChange = currIndex;
}
}
currIndex++;
}
Console.WriteLine(count);
Console.ReadLine();
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.