Skip to content

Commit

Permalink
ecursively-remove-all-adjacent-duplicates
Browse files Browse the repository at this point in the history
recursively-remove-all-adjacent-duplicates
  • Loading branch information
hraverkar committed Apr 1, 2019
1 parent e492b14 commit 2a48be2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Remove_Adjacent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
namespace ConsoleApplication33
{
internal class Program
{
public static void Main(string[] args)
{
var ab = "geeksforgeek";
var t=RemoveAdjcent(ab);
Console.WriteLine(t);
}

private static string RemoveAdjcent(string ab)
{
if (ab.Length == 0) return ab;

var t = ab.ToCharArray();
var lastchar = t[0];

var count = 1;
for (var i = 1; i < ab.Length; i++)
{
if (count > 0 && t[i] == t[count - 1])
{
lastchar = t[count - 1];

while (count > 0 && t[count - 1] == lastchar)
{
count--;
}
}
else if (t[i] == lastchar)
{

}
else
{
t[count++] = t[i];
}
}
return new string(t,0,count);
}
}
}

0 comments on commit 2a48be2

Please sign in to comment.