Remove All Adjacent Duplicates in String II Last Updated : 10 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string s and an integer k, the task is to repeatedly delete k adjacent duplicates till no deletions are possible and then return the final string. On deletion of k adjacent duplicates, the left and right sides of the deleted substring is concatenated together. Examples: Input: s = "abcd", k = 2Output: "abcd"Explanation: There's nothing to delete. Input: s = "deeedbbcccbdaa", k = 3Output: "aa"Explanation: First delete "eee" and "ccc", get "ddbbbdaa".Then delete "bbb", get "dddaa".Finally delete "ddd", get "aa".Approach: To solve the problem, follow the below idea: To solve this problem, we can use a stack to keep track of characters and their counts. The idea is to use the stack to store pairs of characters and their consecutive counts. Whenever we encounter k consecutive characters, we remove them from the stack. Step-by-step algorithm: Initialize a stack that will store pairs of characters and their consecutive counts.Iterate through the string:For each character, check if the stack is not empty and the top character of the stack is the same as the current character.If they are the same, increment the count.If the count reaches k, pop the stack.If they are not the same, push the current character with a count of 1 onto the stack.Construct the resulting string from the stack.Return the resulting string.Below is the implementation of the algorithm: C++ #include <bits/stdc++.h> using namespace std; string removeDuplicates(string s, int k) { // stack to store the character with its count stack<pair<char, int> > st; for (char c : s) { // If the character is same as character at top, increase the count of top character if (!st.empty() && st.top().first == c) { st.top().second++; if (st.top().second == k) { st.pop(); } } // If the character is different from the character at top, push the character to the top else { st.push({ c, 1 }); } } string result = ""; // Construct the result string while (!st.empty()) { result.append(st.top().second, st.top().first); st.pop(); } reverse(result.begin(), result.end()); return result; } int main() { // Example 1 string s1 = "abcd"; int k1 = 2; cout << removeDuplicates(s1, k1) << endl; // Output: "abcd" return 0; } Java import java.util.*; public class RemoveDuplicates { public static String removeDuplicates(String s, int k) { // Stack to store the character with its count Deque<Pair> stack = new ArrayDeque<>(); for (char c : s.toCharArray()) { // If the character is same as the character at top, increase the count of top character if (!stack.isEmpty() && stack.peek().character == c) { stack.peek().count++; if (stack.peek().count == k) { stack.pop(); } } // If the character is different from the character at top, push the character to the top else { stack.push(new Pair(c, 1)); } } StringBuilder result = new StringBuilder(); // Construct the result string while (!stack.isEmpty()) { Pair p = stack.pop(); for (int i = 0; i < p.count; i++) { result.append(p.character); } } return result.reverse().toString(); } public static void main(String[] args) { // Example 1 String s1 = "abcd"; int k1 = 2; System.out.println(removeDuplicates(s1, k1)); // Output: "abcd" } } class Pair { char character; int count; Pair(char character, int count) { this.character = character; this.count = count; } } // This code is contributed by Shivam Gupta Outputabcd Time Complexity: O(n), where n is the length of the string. Auxiliary Space: O(n) Related Articles: Remove duplicates from a given stringRemove all duplicate adjacent characters from a string using StackRecursively remove all adjacent duplicates Comment More infoAdvertise with us Next Article Remove All Duplicates from a Given String in Python C codernitnucy Follow Improve Article Tags : Stack DSA Bloomberg Interview-Questions Practice Tags : BloombergStack Similar Reads Recursively remove all adjacent duplicates Given a string S, the task is to remove all its adjacent duplicate characters recursively. Examples: Input: S = "geeksforgeek"Output: "gksforgk"Explanation: g(ee)ksforg(ee)k -> gksforgkInput: S = "abccbccba"Output: ""Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->"" (empty s 13 min read Remove All Duplicates from a Given String in Python The task of removing all duplicates from a given string in Python involves retaining only the first occurrence of each character while preserving the original order. Given an input string, the goal is to eliminate repeated characters and return a new string with unique characters. For example, with 2 min read Remove duplicates from a string Given a string s which may contain lowercase and uppercase characters. The task is to remove all duplicate characters from the string and find the resultant string. Note: The order of remaining characters in the output should be the same as in the original string.Example:Input: s = geeksforgeeksOutp 10 min read Remove all duplicate adjacent characters from a string using Stack Given a string, str, the task is to remove all the duplicate adjacent characters from the given string. Examples: Input: str= âazxxzyâOutput: ay Removal of "xx" modifies the string to âazzyâ. Now, the removal of "zz" modifies the string to âayâ. Since the string âayâ doesn't contain duplicates, the 6 min read Remove duplicates from a string in O(1) extra space Given a string str of lowercase characters, the task is to remove duplicates and return a resultant string without modifying the order of characters in the original string. Examples: Input: str = "geeksforgeeks" Output: geksfor Input: str = "characters" Output: chartes Approach: The idea is to use b 13 min read Remove all consecutive duplicates from the string Given a string s , The task is to remove all the consecutive duplicate characters of the string and return the resultant string. Examples: Input: s = "aaaaabbbbbb"Output: abExplanation: Remove consecutive duplicate characters from a string s such as 5 a's are at consecative so only write a and same 10 min read Like