Recursively remove all adjacent duplicates | Practice | GeeksforGeeks

Given a string s, remove all its adjacent duplicate characters recursively. 
Example 1:
Input:
S = "geeksforgeek"
Output: "gksforgk"
Explanation: 
g(ee)ksforg(ee)k -> gksforgk

Example 2:
Input: 
S = "abccbccba"
Output: ""
Explanation: 
ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string)

Your Task:
You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string.
Note: For some test cases, the resultant string would be an empty string. For that case, the function should return the empty string only.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)

Constraints:
1<=|S|<=105
+1
shreyank3453 days ago
my code:
string res="";       int k=0;       while(s[k]){           if(s[k]!=s[k+1]){               res.push_back(s[k]);               ++k;           }           if(s[k+1] && s[k]==s[k+1]){               while(s[k+1] && s[k]==s[k+1]){                   ++k;               }               ++k;           }       }       if(res==s){           return res;       }       else{       return remove(res);       }
0
vishaldasani4 days ago
string remove(string s){       // code here     int i=0,n=s.size();     string temp;     while(i<n)     {         int j=i+1;         while(s[i]==s[j]&&j<n)         j++;         if(j==i+1)temp+=s[i];         i=j;     }     if(s.size()==temp.size())     return temp;     else     return remove(temp);                  }
0
mohankumarit20014 days ago
class Solution{
    string rem(string &s){
        string res = "";
        int i=0,j=0;
        while(i<s.size()){
            j=i+1;
            while(j<s.size() && s[i]==s[j])j++;
            if(i+1==j){
                res.push_back(s[i]);
                i++;
            }
            else i=j;
        }
        if(s!=res)return rem(res);
        return res;
    }
public:
    string remove(string &s){
        // code here
        return rem(s);
    }
};
+1
siddhant4583agarwalPremium4 days ago
if(s.size()==0||s.size()==1){
            return s;
        }
        string temp;
        if(s[0]!=s[1]){
            temp.push_back(s[0]);
        }
        for(int i=1;i<s.size()-1;i++){
            if(s[i]!=s[i+1] && s[i]!=s[i-1]){
                temp.push_back(s[i]);
            }
        }
        if(s[s.size()-1]!=s[s.size()-2]){
            temp.push_back(s[s.size()-1]);
        }
        // cout<<temp<<endl;
        if(temp==s){
            return temp;
        }
        else{
            return remove(temp);
        }
0
shalini21sirothiya4 days ago
//c++
 string remove(string s){              while(1){      string temp;      int i=0;int n=s.length();      while(i<n){          int j=i+1;          while(s[i]==s[j] && j<n){              j++;          }          if(j==i+1)temp+=s[i];          i=j;      }      if(s.size()==temp.size())break;      s=temp;      }      return s;   }
0
nikhilgoyalmp4 days ago
Easy C++ solution 
Space Complexity O(1)
Time complexity O(|S|)
class Solution{public:   string remove(string s)   {       int i,count=0;       for(i=0;i<s.length();i++)       {           if(s[i]==s[i+1])           {               int j=i;               while(s[i+1]==s[i])               {                   i++;               }               s.erase(j,i+1-j);               i=i-(i-j)-1;               count++;           }       }       if(count==0)       {return s;}       else       {return remove(s);}   }};
+1
yashchawla1164 days ago
Simple To Understand And Easy To Implement.
 
https://yashboss116.blogspot.com/2022/04/recursively-remove-all-adjacent.html
 
Approach Explained With Complexities Mentioned.
 
0
gupta2411sumit4 days ago
 bool contains(string s)   {       for( int i = 1 ; i<s.length() ; i++)       {           if(s[i]==s[i-1])           {               return true ;           }       }              return false ;   }   string remove(string s){       // code here              string ans = "" ;       int i = 0 ;              for( i = 0 ; i<s.length()-1 ; i++)       {           if(s[i] ==s[i+1])           {               while(s[i]==s[i+1])                   i++ ;           }           else{               ans += s[i] ;           }       }              if(s[i]!=s[i+1])           {               ans += s[i] ; ;           }                                    if(contains(ans))       {           return remove(ans) ;       }       else{           return ans ;       }             // return ans ;   }};
+1
niteshv7114 days ago
c++ 
string ans;
        int len = s.length();
        if(s[0] != s[1])
            ans.push_back(s[0]);
        bool flag = 0;
        for(int i = 1; i < len; i++){
            while(s[i] == s[i+1]){
                flag = 1;
                i++;
            }
            if(i > 0 && s[i] != s[i-1])
                ans.push_back(s[i]);
        }
        if(flag == 0)
            return ans;
        return remove(ans);
0
cyanglass4 days ago
Simple Short & Straight Approach || Python
 
handle the edge case of length 1
Iterate over the string and Form a new string when adjacent unique found
keep check of any duplicate encountered, if yes, invoke recursion with new string
if no duplicate found, return the new string
 
def remove (self, S):
		if len(S)==1:
		    return S
	    news,flag="",False
	    for i in range(0,len(S)-1):
	        if S[i]!=S[i+1]:
	            if (i>0 and S[i-1]!=S[i]) or i==0:
	                news+=S[i]
	            if i==len(S)-2:  #when last char is unique
	                news+=S[i+1]
	            continue
	        
	        flag=True
	    if flag:
	        return self.remove(news)
	    else:
	        return news
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.