Answers for "remove adjacent duplicate characters"

C#
1

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
Posted by: Guest on May-06-2022

Code answers related to "remove adjacent duplicate characters"

C# Answers by Framework

Browse Popular Code Answers by Language