Problem Statement:
You are given a string S, you have to remove all the consecutive string.
Example
Input: aabb
Output: ab
Solution
The solution is very simple and can be solved using recursion.
Base case will be, if the string is empty return.
else compare the adj character and the present character.
If they are same the characters one to left.
Then call the recursion on string s.
If they are not same, then call the recursion from s+1.
Solution in C++
#include <vector>
#include <algorithm>
//visit www.ProDeveloperTutorial.com for 450+ solved questions
#include <iostream>
#include <string>
#include <unordered_map>
#include <queue>
using namespace std;
void remove_duplicates(char* str)
{
//base case
if (str[0] == '\0')
return;
// if the next adj char are same
if (str[0] == str[1])
{
// shift one character to left
int i = 0;
while (str[i] != '\0')
{
str[i] = str[i + 1];
i++;
}
remove_duplicates(str);
}
// If the adjacent characters are not same
remove_duplicates(str + 1);
}
int main()
{
char S1[] = "aabbccaa";
remove_duplicates(S1);
cout << "The string after removing duplicates are " << S1 << endl;
return 0;
}
Output:
The string after removing duplicates are = abca