Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

remove adjacent duplicate characters

	public static string GreatSolution(string s, int k)
    {
        Stack<int[]> stack = new Stack<int[]>();

        foreach (char ch in s.ToCharArray())
        {
            if (stack.Count!=0 && stack.Peek()[0] == ch)
                stack.Peek()[1]++;
            else 
              	stack.Push(new int[] { ch, 1 });
          
            if (stack.Peek()[1] == k) 
              	stack.Pop();
        }
        var sb = string.Empty;
        while (stack.Count>0)
        {
            int[] top = stack.Pop();
          
            while (top[1]-- > 0)
                sb = ((char)top[0]).ToString() +sb;
        }
        return sb.ToString();
    }
// put the value of k as per required
 
PREVIOUS NEXT
Tagged: #remove #adjacent #duplicate #characters
ADD COMMENT
Topic
Name
6+1 =