# Reduce the string by removing K consecutive identical characters

Given a string ‘str’ and an integer ‘k’, the task is to reduce the string by applying the following operation:

Choose a group of ‘k’ consecutive identical characters and remove them. The operation can be performed any number of times until it is no longer possible.

Finally, print the reduced string.

Examples:

Input: k = 2, str = “geeksforgeeks”
Output: gksforgks
After the removal of 2 consecutive “e” twice,
the string will get reduced to “gksforgks”.

Input: k = 3, str = “qddxxxd”
Output: q
Removal of “xxx” gives “qddd”.
Again, removal of “ddd” gives the final string “q”

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: This problem can be solved using the data structure, Stack.

• Maintain a stack of the characters while iterating through them one by one.
• When there are ‘k’ identical characters at the top of the stack, pop them out of the stack.
• To implement this, every element of the stack will be a pair of character and it’s consecutive frequency. If the character to be pushed is different from the character which is currently at the top of the stack then it’s frequency will be set to “1”.
• Else, if the character is identical to the character at the top of the stack then it’s frequency will be one greater than the frequency of the element at the top of the stack.
• In the end, the characters which are left in the stack will form the resultant string.

Below is the implementation of the above approach:

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function that returns ` `    ``// the reduced string after ` `    ``// performing given operation ` `    ``static` `String remove_k_characters(String st1, ``int` `n, ``int` `k) ` `    ``{ ` ` `  `        ``// Stack to store the character ` `        ``Stack st = ``new` `Stack(); ` ` `  `        ``int` `i = ``0``; ` `        ``for` `(i = ``0``; i < n; i++) { ` ` `  `            ``// the current character ` `            ``char` `x = st1.charAt(i); ` ` `  `            ``// if the stack is not empty ` `            ``// and the frequency of the element ` `            ``// at the top of the stack is = k ` `            ``// then pop k elements ` `            ``if` `(st.size() > ``0` `&& st.peek().frequency == k) { ` ` `  `                ``// stores the character at ` `                ``// the top of the stack ` `                ``char` `curr = st.peek().character; ` ` `  `                ``// while the character ` `                ``// at the top of the stack is curr ` `                ``// pop the character from the stack ` `                ``while` `(st.size() > ``0` `&& st.peek().character == curr) ` `                    ``st.pop(); ` `            ``} ` ` `  `            ``// if the stack is not empty ` `            ``// and the top element of the ` `            ``// stack is = x, ` `            ``if` `(st.size() > ``0` `&& st.peek().character == x) { ` ` `  `                ``// increase it's frequency and ` `                ``// push it to the stack ` `                ``Entity new_top = ``new` `Entity(x, st.peek().frequency + ``1``); ` `                ``st.push(new_top); ` `            ``} ` ` `  `            ``// if the current element is ` `            ``// not equal to the character ` `            ``// at the top of the stack ` `            ``else` `{ ` `                ``Entity new_top = ``new` `Entity(x, ``1``); ` `                ``st.push(new_top); ` `            ``} ` `        ``} ` ` `  `        ``// if the last pushed character ` `        ``// has k frequency, then pop the ` `        ``// top k characters. ` `        ``if` `(st.size() > ``0` `&& st.peek().frequency == k) { ` ` `  `            ``// get the character ` `            ``// at the top of the stack ` `            ``char` `curr = st.peek().character; ` ` `  `            ``// while the character ` `            ``// at the top of the stack is ` `            ``// curr, pop it from the stack ` `            ``while` `(st.size() > ``0` `&& st.peek().character == curr) ` `                ``st.pop(); ` `        ``} ` ` `  `        ``// Stores the string in ` `        ``// reversed fashion from stack ` `        ``String stack_string = ``""``; ` `        ``while` `(st.size() > ``0``) ` `            ``stack_string += st.pop().character; ` ` `  `        ``String reduced_string = ``""``; ` ` `  `        ``// reverse the stack string ` `        ``for` `(i = stack_string.length() - ``1``; i >= ``0``; i--) ` `            ``reduced_string += stack_string.charAt(i); ` ` `  `        ``return` `reduced_string; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `k = ``2``; ` `        ``String st = ``"geeksforgeeks"``; ` `        ``String ans = remove_k_characters(st, st.length(), k); ` `        ``System.out.println(ans); ` `    ``} ` ` `  `    ``static` `class` `Entity { ` `        ``char` `character; ` `        ``int` `frequency; ` `        ``Entity(``char` `p, ``int` `q) ` `        ``{ ` `            ``character = p; ` `            ``frequency = q; ` `        ``} ` `    ``} ` `} `

## C#

 `// C# implementation of the above approach  ` `using` `System; ` `using` `System.Collections.Generic; ` `public` `class` `GFG { ` `  `  `    ``// Function that returns ` `    ``// the reduced string after ` `    ``// performing given operation ` `    ``static` `String remove_k_characters(String st1, ``int` `n, ``int` `k) ` `    ``{ ` `  `  `        ``// Stack to store the character ` `        ``Stack st = ``new` `Stack(); ` `  `  `        ``int` `i = 0; ` `        ``for` `(i = 0; i < n; i++) { ` `  `  `            ``// the current character ` `            ``char` `x = st1[i]; ` `  `  `            ``// if the stack is not empty ` `            ``// and the frequency of the element ` `            ``// at the top of the stack is = k ` `            ``// then pop k elements ` `            ``if` `(st.Count> 0 && st.Peek().frequency == k) { ` `  `  `                ``// stores the character at ` `                ``// the top of the stack ` `                ``char` `curr = st.Peek().character; ` `  `  `                ``// while the character ` `                ``// at the top of the stack is curr ` `                ``// pop the character from the stack ` `                ``while` `(st.Count > 0 && st.Peek().character == curr) ` `                    ``st.Pop(); ` `            ``} ` `  `  `            ``// if the stack is not empty ` `            ``// and the top element of the ` `            ``// stack is = x, ` `            ``if` `(st.Count > 0 && st.Peek().character == x) { ` `  `  `                ``// increase it's frequency and ` `                ``// push it to the stack ` `                ``Entity new_top = ``new` `Entity(x, st.Peek().frequency + 1); ` `                ``st.Push(new_top); ` `            ``} ` `  `  `            ``// if the current element is ` `            ``// not equal to the character ` `            ``// at the top of the stack ` `            ``else` `{ ` `                ``Entity new_top = ``new` `Entity(x, 1); ` `                ``st.Push(new_top); ` `            ``} ` `        ``} ` `  `  `        ``// if the last pushed character ` `        ``// has k frequency, then pop the ` `        ``// top k characters. ` `        ``if` `(st.Count > 0 && st.Peek().frequency == k) { ` `  `  `            ``// get the character ` `            ``// at the top of the stack ` `            ``char` `curr = st.Peek().character; ` `  `  `            ``// while the character ` `            ``// at the top of the stack is ` `            ``// curr, pop it from the stack ` `            ``while` `(st.Count > 0 && st.Peek().character == curr) ` `                ``st.Pop(); ` `        ``} ` `  `  `        ``// Stores the string in ` `        ``// reversed fashion from stack ` `        ``String stack_string = ``""``; ` `        ``while` `(st.Count > 0) ` `            ``stack_string += st.Pop().character; ` `  `  `        ``String reduced_string = ``""``; ` `  `  `        ``// reverse the stack string ` `        ``for` `(i = stack_string.Length - 1; i >= 0; i--) ` `            ``reduced_string += stack_string[i]; ` `  `  `        ``return` `reduced_string; ` `    ``} ` `  `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int` `k = 2; ` `        ``String st = ``"geeksforgeeks"``; ` `        ``String ans = remove_k_characters(st, st.Length, k); ` `        ``Console.WriteLine(ans); ` `    ``} ` `  `  `    ``public` `class` `Entity { ` `        ``public` `char` `character; ` `        ``public` `int` `frequency; ` `        ``public` `Entity(``char` `p, ``int` `q) ` `        ``{ ` `            ``character = p; ` `            ``frequency = q; ` `        ``} ` `    ``} ` `} ` `// This code has been contributed by 29AjayKumar `

Output:

```gksforgks
```

Time Complexity – O(N)
Each element gets pushed into stack only once. My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.