Remove first adjacent pairs of similar characters until possible
Given a string Str, the task is to remove first adjacent pairs of similar characters until we can.
Note: Remove adjacent characters to get a new string and then again remove adjacent duplicates from the new string and keep repeating this process until all similar adjacent character pairs are removed.
Examples:
Input: str = “keexxllx”
Output: kx
Step 0: Remove ee to get “kxxllx”
Step 1: Remove xx to get “kllx”
Step 2: Remove ll to get “kx”Input: str = “abbaca”
Output: ca
Approach:
Use string’s back() and pop_back() method STL in C++ to solve the above problem. Iterate for every character in the string, and if the adjacent characters are same, then remove the adjacent characters using pop_back() function. At the end, return the final string.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to remove adjacent duplicates string removeDuplicates(string S) { string ans = ""; // Iterate for every character // in the string for (auto it : S) { // If ans string is empty or its last // character does not match with the // current character then append this // character to the string if (ans.empty() or ans.back() != it) ans.push_back(it); // Matches with the previous one else if (ans.back() == it) ans.pop_back(); } // Return the answer return ans; } // Driver Code int main() { string str = "keexxllx"; cout << removeDuplicates(str); } |
Python3
# Python3 implementation of the above approach # Function to remove adjacent duplicates def removeDuplicates(S) : ans = ""; # Iterate for every character # in the string for it in S : # If ans string is empty or its last # character does not match with the # current character then append this # character to the string if (ans == "" or ans[-1] != it) : ans += it ; # Matches with the previous one elif (ans[-1] == it) : ans = ans[:-1]; # Return the answer return ans; # Driver Code if __name__ == "__main__" : string = "keexxllx"; print(removeDuplicates(string)); # This code is contributed by AnkitRai01 |
kx
Recommended Posts:
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Permutation of a string with maximum number of characters greater than its adjacent characters
- String with k distinct characters and no same characters adjacent
- Minimum swaps to group similar characters side by side?
- Recursively remove all adjacent duplicates
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Count of strings where adjacent characters are of difference one
- Count of adjacent Vowel Consonant Pairs
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Remove characters that appear more than k times
- Remove even frequency characters from the string
- Remove all characters other than alphabets from string
- Remove all non-alphabetical characters of a String in Java
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.
Improved By : AnkitRai01



