Table of contents
1.
Introduction
2.
Approach
3.
Implementation
4.
Frequently Asked Questions
4.1.
What are adjacent duplicates in a string?
4.2.
How do I recursively remove adjacent duplicates from a string?
4.3.
How do you remove all duplicates from a string solution?
5.
Conclusion
Last Updated: Oct 23, 2024
Easy

Recursively remove all adjacent duplicates

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The programs involving strings serve as crucial interview questions in many companies to date. In this tutorial, we will discuss the concept of the C++ programming language using which we will code how to recursively remove all adjacent duplicate characters in a string. We will take an input for which the program will return the desired output. For example, moon. We have to remove all the adjacent duplicate characters till there are none. The output of the above input will be mn. Here are a few more examples. 

Recursively remove all adjacent duplicates


webber - wr (First, both the b's are removed, then both the e's)
reeree - empty string(First two e's are removed, then the last two e's and both the r's)

Approach

Suppose we take a string s of length n. We will start from the leftmost character and remove duplicates at the leftmost corner, if any, ensuring the first character is different from its adjacent. Repeat the same for the remaining string of length n-1. Let the reduced string be s2. There are the following cases possible.

  1. The first character of s2 matches s. Then, remove that character from s2.
  2. The s2 string becomes empty since the last removed character is the same as the first character of s. Then the resultant output will be an empty string.
  3. If the above-mentioned cases do not work, append the first character of s at the beginning of s2. 
    Then, return s2 as the output.

Time Complexity: O(n)

Also read, Application of Oops

Implementation

#include <iostream>
#include <string.h>
using namespace std;

char* removeHelper(char *str, char *last_char){
     
    if (str[0] == '\0' || str[1] == '\0')
        return str;


    if (str[0] == str[1]){
        *last_char = str[0];
        while (str[1] && str[0] == str[1])
            str++;
        str++;
        return removeHelper(str, last_char);
    }
    char* newStr = removeHelper(str+1,last_char);
    if (newStr[0] && newStr[0] == str[0]){
        *last_char = str[0];
        return (newStr+1);
    }

    if (newStr[0] == '\0' && *last_char == str[0])
        return newStr;
    newStr--;
    newStr[0] = str[0];
    return newStr;
}
 
char *remove(char *str){
    char last_char = '\0';
    return removeHelper(str, &last_char);
}
 
int main(){
    char str[] = "acbbcddf";
    cout << remove(str) << endl;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

af

 

You can try by yourself with the help of online C++ Compiler.

Also read -  Decimal to Binary c++

Frequently Asked Questions

What are adjacent duplicates in a string?

Two adjacent characters in a string are adjacent duplicate characters if they are the same.

How do I recursively remove adjacent duplicates from a string?

To recursively remove adjacent duplicates, compare consecutive characters in the string. If duplicates are found, remove them and recursively call the function on the new string. Repeat this process until no adjacent duplicates remain, returning the final output.

How do you remove all duplicates from a string solution?

To remove all duplicates from a string, identify the unique characters by processing each character only once. Then, combine these unique characters into a new string, ensuring that each character appears only once in the final output.

Conclusion

In this blog, we learned the concept of the C++ programming language using which we could code how to recursively remove all adjacent duplicate characters in a string. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out Code360. 

Live masterclass