8. Super Reduced String in C#
The following sequence of operations to get the final string.
aaabccddd → abccddd → abddd → abd
aa → Empty String
baab → bb → Empty String
using System;
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int top = -1;
char[] remains = new char[input.Length];
foreach (var c in input)
{
if (top < 0 || c != remains[top])
{
remains[++top] = c;
}
else
{
top--;
}
}
var data= top < 0 ? "Empty String" : new String(remains, 0, top+1);
Console.WriteLine(data);
Console.ReadLine();
}
}
output
aaabccddd
abd