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++
#include <bits/stdc++.h>
using namespace std;
string removeDuplicates(string S)
{
string ans = "" ;
for ( auto it : S) {
if (ans.empty() or ans.back() != it)
ans.push_back(it);
else if (ans.back() == it)
ans.pop_back();
}
return ans;
}
int main()
{
string str = "keexxllx" ;
cout << removeDuplicates(str);
}
|
Java
class GFG
{
static String removeDuplicates(String S)
{
String ans = "" ;
for ( int i = 0 ; i < S.length(); i++)
{
if (ans.isEmpty() ||
ans.charAt(ans.length() - 1 ) != S.charAt(i))
ans += S.charAt(i);
else if (ans.charAt(ans.length() - 1 ) == S.charAt(i))
ans = ans.substring( 0 , ans.length() - 1 );
}
return ans;
}
public static void main(String[] args)
{
String str = "keexxllx" ;
System.out.println(removeDuplicates(str));
}
}
|
Python3
def removeDuplicates(S) :
ans = "";
for it in S :
if (ans = = "" or ans[ - 1 ] ! = it) :
ans + = it ;
elif (ans[ - 1 ] = = it) :
ans = ans[: - 1 ];
return ans;
if __name__ = = "__main__" :
string = "keexxllx" ;
print (removeDuplicates(string));
|
C#
using System;
class GFG
{
static String removeDuplicates(String S)
{
String ans = "" ;
for ( int i = 0; i < S.Length; i++)
{
if (ans == "" ||
ans[ans.Length - 1] != S[i])
ans += S[i];
else if (ans[ans.Length - 1] == S[i])
ans = ans.Substring(0, ans.Length - 1);
}
return ans;
}
public static void Main(String[] args)
{
String str = "keexxllx" ;
Console.WriteLine(removeDuplicates(str));
}
}
|
Javascript
<script>
function removeDuplicates( S) {
var ans = "" ;
for (i = 0; i < S.length; i++) {
if (ans.length==0 ||
ans.charAt(ans.length - 1) != S.charAt(i))
ans += S.charAt(i);
else if (ans.charAt(ans.length - 1) == S.charAt(i))
ans = ans.substring(0, ans.length - 1);
}
return ans;
}
var str = "keexxllx" ;
document.write(removeDuplicates(str));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the length of the string.
Auxiliary Space: O(N), as we are using extra space for the ans string. Where N is the length of the string.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Jun, 2022
Like Article
Save Article