Given a string S, the task is to find the resultant smallest string possible, formed by repeatedly removing pairs of adjacent characters which are equal.
Examples:
Input: S = “aaabccddd”
Output: abd
Explanation: Following sequence of removal of pairs of adjacent characters minimizes the length of the string:
aaabccddd ? abccddd ? abddd ? abd.
Input: S = aa
Output: Empty String
Explanation: Following sequence of removal of pairs of adjacent characters minimizes the length of the string:
aa ? “”.
Approach: The main idea to solve the given problem is to recursively delete all pairs of adjacent characters which are equal, one by one. Follow the steps to solve the given problem:
- Initialize an empty string, say ans, to store the string of minimum length after deleting all pairs of equal adjacent characters.
- Initialize a string, say pre, to store the updated string after every removal of equal adjacent characters.
- Now, iterate until the string ans and pre are unequal and perform the following steps:
- Update the value of the string ans by removing the first adjacent same character using the function removeAdjacent().
- If the value of the string ans is the same as the string pre, then break out of the loop. Otherwise, update the value of string pre as the string ans.
- After completing the above steps, print the string ans as the resultant string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string removeAdjacent(string s)
{
if (s.length() == 1)
return s;
string sb = "" ;
for ( int i = 0; i < s.length() - 1; i++)
{
char c = s[i];
char d = s[i + 1];
if (c != d)
{
sb = sb + c;
if (i == s.length() - 2)
{
sb = sb + d;
}
}
else
{
for ( int j = i + 2;
j < s.length(); j++)
sb = sb + s[j];
return sb;
}
}
return sb;
}
void reduceString(string s)
{
string result = "" ;
string pre = s;
while ( true )
{
result = removeAdjacent(pre);
if (result == pre)
break ;
pre = result;
}
if (result.length() != 0)
cout << (result) << endl;
else
cout << "Empty String" << endl;
}
int main()
{
string S = "aaabccddd" ;
reduceString(S);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static String removeAdjacent(String s)
{
if (s.length() == 1 )
return s;
StringBuilder sb = new StringBuilder( "" );
for ( int i = 0 ;
i < s.length() - 1 ; i++) {
char c = s.charAt(i);
char d = s.charAt(i + 1 );
if (c != d) {
sb.append(c);
if (i == s.length() - 2 ) {
sb.append(d);
}
}
else {
for ( int j = i + 2 ;
j < s.length(); j++)
sb.append(s.charAt(j));
return sb.toString();
}
}
return sb.toString();
}
public static void reduceString(String s)
{
String result = "" ;
String pre = s;
while ( true ) {
result = removeAdjacent(pre);
if (result.equals(pre))
break ;
pre = result;
}
if (result.length() != 0 )
System.out.println(result);
else
System.out.println( "Empty String" );
}
public static void main(String[] args)
{
String S = "aaabccddd" ;
reduceString(S);
}
}
|
Python3
def removeAdjacent(s):
if ( len (s) = = 1 ):
return s
sb = ""
for i in range ( len (s) - 1 ):
c = s[i]
d = s[i + 1 ]
if (c ! = d):
sb = sb + c
if (i = = len (s) - 2 ):
sb = sb + d
else :
for j in range (i + 2 , len (s)):
sb = sb + s[j]
return sb
return sb
def reduceString(s):
result = ""
pre = s
while True :
result = removeAdjacent(pre)
if (result = = pre):
break
pre = result
if ( len (result)! = 0 ):
print (result)
else :
print ( "Empty String" )
S = "aaabccddd"
reduceString(S)
|
C#
using System;
class GFG {
static string removeAdjacent( string s)
{
if (s.Length == 1)
return s;
string sb = "" ;
for ( int i = 0; i < s.Length - 1; i++)
{
char c = s[i];
char d = s[i + 1];
if (c != d)
{
sb = sb + c;
if (i == s.Length - 2)
{
sb = sb + d;
}
}
else
{
for ( int j = i + 2;
j < s.Length; j++)
sb = sb + s[j];
return sb;
}
}
return sb;
}
static void reduceString( string s)
{
string result = "" ;
string pre = s;
while ( true )
{
result = removeAdjacent(pre);
if (result == pre)
break ;
pre = result;
}
if (result.Length != 0)
Console.WriteLine(result);
else
Console.WriteLine( "Empty String" );
}
static void Main() {
string S = "aaabccddd" ;
reduceString(S);
}
}
|
Javascript
<script>
function removeAdjacent(s)
{
if (s.length == 1)
return s;
let sb = "" ;
for (let i = 0; i < s.length - 1; i++)
{
let c = s[i];
let d = s[i + 1];
if (c != d)
{
sb = sb + c;
if (i == s.length - 2)
{
sb = sb + d;
}
}
else
{
for (let j = i + 2; j < s.length; j++)
sb = sb + s[j];
return sb;
}
}
return sb;
}
function reduceString(s)
{
let result = "" ;
let pre = s;
while ( true )
{
result = removeAdjacent(pre);
if (result == pre)
break ;
pre = result;
}
if (result.length != 0)
document.write(result);
else
document.write( "Empty String" );
}
let S = "aaabccddd" ;
reduceString(S);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
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!