
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Remove All Adjacent Duplicates in String II in C++
Suppose a string s is given, a k duplicate removal consists of choosing k adjacent and equal letters from string s and removing them causing the left and the right side of the deleted substring to concatenate together. We will repeatedly make k duplicate removals on the given string s until we cannot change any remaining. We have to find the final string after all such duplicate removals have been made. So if the input is like s = “deeedbbcccbdaa”, and k = 3, then the output will be “aa”, at first delete the “eee” and “ccc” and we will get “ddbbbaa”, then delete “bbb”, the string will be “dddaa”, then delete “ddd”, and the output will be “aa”
To solve this, we will follow these steps −
- ans := empty string
- create one stack for char-int pair, n := size of the string
- for i in range 0 to n
- x := s[i]
- if stack is not empty and integer of the stack top element = k, then delete top element from stack
- if i = n, then break
- if stack is empty or character of stack top is not x, then insert pair (x, 1) into stack, and increase i by 1
- otherwise increase the integer part of the stack top element, and increase i by 1
- while stack is not empty
- temp := stack top element
- while integer part of temp is not 0,
- ans := ans + character part of temp
- decrease integer part of temp by 1
- delete the top element from stack
- reverse the ans string and return.
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: string removeDuplicates(string s, int k) { string ans = ""; stack < pair<char, int> > st; int n = s.size(); for(int i = 0; i <= n;){ char x = s[i]; if(!st.empty() && st.top().second == k)st.pop(); if(i == n)break; if(st.empty() || st.top().first != x){ st.push({x, 1}); i++; } else { st.top().second++; i++; } } while(!st.empty()){ pair <char, int> temp = st.top(); while(temp.second--) ans += temp.first; st.pop(); } reverse(ans.begin(), ans.end()); return ans; } }; main(){ Solution ob; cout <<(ob.removeDuplicates("deeedbbcccbdaa", 3)); }
Input
"deeedbbcccbdaa" 3
Output
aa
- Related Articles
- Remove All Adjacent Duplicates In String in Python
- Remove all duplicates from a given string in C#
- Remove all duplicates from a given string in Python
- Remove Duplicates from Sorted Array II in C++
- Remove Duplicates from Sorted List II in C++
- Removing adjacent duplicates from a string in JavaScript
- Print a closest string that does not contain adjacent duplicates in C++
- Print all the duplicates in the input string in C++
- Remove duplicates from a List in C#
- Remove Duplicates from Sorted List in C++
- C# program to remove all duplicates words from a given sentence
- Print all distinct permutations of a given string with duplicates in C++
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Remove Consecutive Duplicates in Python
- Find All Duplicates in an Array in C++
