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
0
prachiyadav1321in 2 hours
def rremove (self, S):
		
		new=[]
        i=0
        n=len(S)
        while i<len(S):
            temp=0
            while (i<n-1 and S[i]==S[i+1]):
                temp=1
                i+=1
            if temp==0:
                new.append(S[i])
            i+=1
        if len(new)<n:
            return self.rremove(new)
        return ''.join(new)
0
deepak609tiwari3 hours ago
JAVA CODE
 
boolean flag;
        do {
            flag = false;
        	StringBuilder sb = new StringBuilder("");
        	int len = s.length();
    
        	if(len <= 1)
            	return s;
        	if(s.charAt(0) != s.charAt(1)) 
            	sb.append(s.charAt(0));
        
        	for(int i=1;i<len-1;i++){
            	char ch = s.charAt(i);
            	if(ch != s.charAt(i-1) && ch != s.charAt(i+1))
                	sb.append(ch);
            	else 
                	flag = true;
        	}
        	if(s.charAt(len-1) != s.charAt(len-2))
            	sb.append(s.charAt(len-1));
            
        	len = sb.length();
        	s = sb.toString();
    	}while(flag);
        return s; 
0
thiruvazhidhinesh9 hours ago
JAVA SOLUTION:
class Solution{   String rremove(String s) {       return call(s);   }   String call(String s){       StringBuilder sb=new StringBuilder(s);       int size=sb.length();       for(int i=0;i<size-1;i++){           if(sb.charAt(i)==sb.charAt(i+1)){               int k=i;               while(k<size-1 && sb.charAt(k)==sb.charAt(k+1)){                   k++;               }               sb.delete(i,k+1);               size=sb.length();               i=i-1;           }       }       if(s.equals(String.valueOf(sb))){           return s;       }       return call(String.valueOf(sb));   }}
0
thiruvazhidhinesh
This comment was deleted.
0
akhilcool2 days ago
bool palindrom(string s){
        int i=0,j=s.size()-1;
        while(i<j){
            if(s[i]!=s[j]) return false;
            i++,j--;
        }
        return true;
    }
    void  removedub(string &s){
        string ans="";
        for(int i=0;i<s.size();i++){
            if(s[i]!='@') ans+=s[i];
        }
        s=ans;
    }
    void solve(string &s,int idx){
        
       
        for(int i=idx+1;i<s.size();i++){
            char a=s[i-1];
            if(s[i]==a){
                s[i-1]='@';
               while(i<s.size() and s[i]==a){
                   s[i]='@';i++;
               }
            }
        }
       
        string ans=s;
        removedub(s);
        if(ans==s) return ;
        solve(s,0);
       
    }
    string rremove(string s){
      if(palindrom(s)) return "";
      solve(s,0);
      return s;
    }
0
ujjwalagarwal8634 days ago
class Solution{
public:
    void fun(string& ans,string& s,int& i,bool& f,char& c){
        if(i==s.length()){
            if(ans.size() and c!='?') ans.pop_back();
            
            return;
        }
        
        if(ans.size() and ans.back() == s[i]){
            c = s[i];
            f = true;
            i++;
        }else if(c != '?'){
            if(ans.size() )ans.pop_back();
            ans += s[i];
            c = '?';
            i++;
        }else{
            ans += s[i];
            i++;
        }
        
        fun(ans,s,i,f,c);
    }
    string rremove(string s){
        // code here
        string ans;
        bool f = true;
        while(f){
            f = false;
            int i = 0;
            char c = '?';
            fun(ans,s,i,f,c);
            s = ans;
            // cout<<ans<<" ";
            ans.clear();
        }
        return s;
    }
};
0
ironman0071 week ago
string rremove(string s){
        if(s.length()==1)
            return s;
        int i=0, n=s.length();
        string temp="";
        bool rem=false; // if any part of string removed in an iteration
        while(i<n){
            if(i<n-1 && s[i]==s[i+1]){
                while(i<n-1 && s[i]==s[i+1])
                    i++;
                rem=true;
            }
            else
                temp+=s[i];
            i++;
        }
        // cout<<temp<<" ";
        if(rem==true)// found removing character in this string
            return rremove(temp);
        else
            return temp;
    }
0
17_7_31 week ago
string rremove(string s){
        // code here
        string res = "";
        int n = s.length();
        
        int i=0;
        while(i<n){
            char c = s[i];
            int freq = 0;
            while(s[i] == c)
                freq++, i++;
            
            if(freq == 1)
                res += c;
        }
        
        if(res == s)
            return res;
        return rremove(res);
}
+1
sirohianirudh91 week ago
string rem(string s){   string ans;   int n=s.size();   int i=0;   while(i<n)   {       if(i<n-1&&s[i]==s[i+1])       {           while(i<n-1&&s[i]==s[i+1])           i++;       }       else ans.push_back(s[i]);       i++;   }   return ans;}   string rremove(string s)   {       string s1;       while(s.size()!=s1.size())       {           s1=s;           s=rem(s);       }       return rem(s);   }
0
vashistabasava1 week ago
//Used a queue to remove the adjacent duplicate characters. 
 
class Solution{  public String remove(String s) { if(s.length()==0)  return ""; ArrayDeque <Character> stack=new ArrayDeque<Character>(); stack.add(s.charAt(0)); StringBuffer sb=new StringBuffer(); boolean temp=false; for(int i=1;i<s.length();i++) {   if(stack.peekLast()==s.charAt(i)) {   if(i==s.length()-1)   {    stack.pollLast();    temp=false;    break;   }   temp=true;   continue;  }  else {   if(temp) {    stack.pollLast();    stack.add(s.charAt(i));    temp=false;   }   else    stack.add(s.charAt(i));  } } while(!stack.isEmpty()) {  sb.append(stack.poll()); } return new String(sb);}   String rremove(String s) {       // code here    int size=s.length();    int currSize=s.length();     do{      size=s.length();     s=remove(s);     currSize=s.length();    }while(size!=currSize);    return s;       }}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.