Remove all duplicate adjacent characters from a string using Stack
Last Updated :
01 Sep, 2021
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 output is ay.
Input: “aaccdd”
Output: Empty String
Recursive Approach: Refer to the article Recursively remove all adjacent duplicates to solve this problem recursively.
Time Complexity: O(N)
Auxiliary Space: O(N)
String Functions-based Approach: Refer to this article Remove first adjacent pairs of similar characters until possible to solve this problem using inbuilt functions pop_back() and back() methods of string.
Time Complexity: O(N)
Auxiliary Space: O(N)
Stack-based Approach: The problem can be solved using Stack to use the property of LIFO. The idea is to traverse the string from left to right and check if the stack is empty or the top element of the stack is not equal to the current character of str, then push the current character into the stack. Otherwise, pop the element from the top of the stack. Follow the steps below to solve the problem:
- Create a stack, st to remove the adjacent duplicate characters in str.
- Traverse the string str and check if the stack is empty or the top element of the stack not equal to the current character. If found to be true, push the current character into st.
- Otherwise, pop the element from the top of the stack.
- Finally, print all the remaining elements of the stack.
C++
#include <bits/stdc++.h>
using namespace std;
string ShortenString(string str1)
{
stack< char > st;
int i = 0;
while (i < str1.length())
{
if (st.empty() || str1[i] != st.top())
{
st.push(str1[i]);
i++;
}
else
{
st.pop();
i++;
}
}
if (st.empty())
{
return ( "Empty String" );
}
else
{
string short_string = "" ;
while (!st.empty())
{
short_string = st.top() +
short_string;
st.pop();
}
return (short_string);
}
}
int main()
{
string str1 = "azzxzy" ;
cout << ShortenString(str1);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String ShortenString(String str1)
{
Stack<Character> st =
new Stack<Character>();
int i = 0 ;
while (i < str1.length())
{
if (st.isEmpty() ||
str1.charAt(i) != st.peek())
{
st.add(str1.charAt(i));
i++;
}
else
{
st.pop();
i++;
}
}
if (st.isEmpty())
{
return ( "Empty String" );
}
else
{
String short_String = "" ;
while (!st.isEmpty())
{
short_String = st.peek() +
short_String;
st.pop();
}
return (short_String);
}
}
public static void main(String[] args)
{
String str1 = "azzxzy" ;
System.out.print(ShortenString(str1));
}
}
|
Python3
def ShortenString(str1):
st = []
i = 0
while i < len (str1):
if len (st) = = 0 or str1[i] ! = st[ - 1 ]:
st.append(str1[i])
i + = 1
else :
st.pop()
i + = 1
if len (st) = = 0 :
return ( "Empty String" )
else :
short_string = ""
for i in st:
short_string + = str (i)
return (short_string)
if __name__ = = "__main__" :
str1 = "azzxzy"
print (ShortenString(str1))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static String ShortenString(String str1)
{
Stack< char > st = new Stack< char >();
int i = 0;
while (i < str1.Length)
{
if (st.Count == 0 || (st.Count != 0 &&
str1[i] != st.Peek()))
{
st.Push(str1[i]);
i++;
}
else
{
if (st.Count != 0)
st.Pop();
i++;
}
}
if (st.Count == 0)
{
return ( "Empty String" );
}
else
{
String short_String = "" ;
while (st.Count != 0)
{
short_String = st.Peek() +
short_String;
st.Pop();
}
return (short_String);
}
}
public static void Main(String[] args)
{
String str1 = "azzxzy" ;
Console.Write(ShortenString(str1));
}
}
|
Javascript
<script>
function ShortenString(str1)
{
var st = [];
var i = 0;
while (i < str1.length)
{
if (st.length==0 || str1[i] != st[st.length-1])
{
st.push(str1[i]);
i++;
}
else
{
st.pop();
i++;
}
}
if (st.length==0)
{
return ( "Empty String" );
}
else
{
var short_string = "" ;
while (st.length!=0)
{
short_string = st[st.length-1] +
short_string;
st.pop();
}
return (short_string);
}
}
var str1 = "azzxzy" ;
document.write( ShortenString(str1));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...