Remove Consecutive Characters | Practice | GeeksforGeeks

Given a string S delete the characters which are appearing more than once consecutively.
Example 1:
Input:
S = aabb
Output:  ab 
Explanation: 'a' at 2nd position is
appearing 2nd time consecutively.
Similiar explanation for b at
4th position.


Example 2:
Input:
S = aabaa
Output:  aba
Explanation: 'a' at 2nd position is
appearing 2nd time consecutively.
'a' at fifth position is appearing
2nd time consecutively.
 
Your Task:
You dont need to read input or print anything. Complete the function removeConsecutiveCharacter() which accepts a string as input parameter and returns modified string. 
 
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
 
Constraints:
1<=|S|<=105
All characters are lowercase alphabets.
 
0
manavkanjiyanijobupdate2 weeks ago
JAVA SOLUTION ALL TEST CASE PASS
class Solution{   public String removeConsecutiveCharacter(String S){                     char ch = S.charAt(0);       String s = String.valueOf(S.charAt(0));       for(int i = 1 ; i < S.length(); i++)       {           if(S.charAt(i) == ch)           {               ch = S.charAt(i);            }           else           {               ch = S.charAt(i);               s = s + S.charAt(i);           }       }       return s;   }}
+3
sarthakgupta102 weeks ago
class Solution{
    public:
    string removeConsecutiveCharacter(string S)
    {
        // code here.
        string ans;
        for(int i=0;i<S.length();i++)
        {
            if(S[i]!=S[i+1])
            {
                ans+=S[i];
            }
        }
        return ans;
    }
};
simplest solution
+1
mishra07adi3 weeks ago
JAVA Simple Approach:
Total Time Taken: 0.79/3.21
 
class Solution
{
    public String removeConsecutiveCharacter(String S)
    {
        String ans="";
        int n = S.length();
        char c = S.charAt(0);
        ans+=c;
        for(int i=1;i<n;i++)
        {
            if(c!=S.charAt(i))
                ans = ans + S.charAt(i);
            
            c = S.charAt(i);
        }
        return ans;
    }
}
+2
mishra07adi3 weeks ago
Fastest and Simplest method
class Solution
{
    public:
    string removeConsecutiveCharacter(string S)
    {
        // code here.
        string ans;
        int n = S.length();
        char c = S[0];
        ans+=c;
        for(int i=1;i<n;i++)
        {
            if(c!=S[i])
                ans=ans+S[i];
            
            c=S[i];
        }
        return ans;
    }
}; 
0
dhanushkorrapati55434 weeks ago
JAVA SOLUTION:
class Solution{   public String removeConsecutiveCharacter(String S){       ArrayList<Character>set=new ArrayList<>();       String ans="";       for(int i=0;i<S.length()-1;i++){           if(S.charAt(i)!=S.charAt(i+1)){               ans=ans+S.charAt(i);           }       }       ans=ans+S.charAt(S.length()-1);       return ans;   }}
+4
amarrajsmart1971 month ago
  string removeConsecutiveCharacter(string S)   {       // code here.       string ans="";       ans+=S[0];       for(int i=1;i<S.length();i++)       {          if(S[i]!=S[i-1])          ans+=S[i];                }       return ans;   }
+1
ashutos17sharma89891 month ago
Recursion in java
class Solution{
    public String removeConsecutiveCharacter(String S){
        String ans="";
        if(S.length()==0||S.length()==1){
            return S;
        }
        int i=0;
        if(S.charAt(i)!=S.charAt(i+1) && i<S.length()-1){
            ans+=S.charAt(0);
        }
        ans+=removeConsecutiveCharacter(S.substring(1));
        return ans;
    }
}
0
blackwolf1431 month ago
 string t;      stack<char>st;      st.push(s[0]);      for(int i=1;i<s.size();++i){        if(st.top()!=s[i]) st.push(s[i]);      }      while(!st.empty()){          t.push_back(st.top());          st.pop();          }          reverse(t.begin(),t.end());          return t;
0
vc1825711 month ago
Java Easy Solution 
   int len=S.length();       String str="";       int i=0;       for(i=0;i<len-1;i++){           if(S.charAt(i)!=S.charAt(i+1)){               str+=S.charAt(i);           }       }       str+=S.charAt(i);       return str;
+1
prashr11 month ago
Solution with O(1) auxillary space and O(|S|) Time complexity:
class Solution{
    public:
    string removeConsecutiveCharacter(string S)
    {
        // code here.
        if(!S.length())
            return S;
        char x=S[0];
        for(int i=1;i<S.length();i++)
        {
            if(S[i]==x)
            {
                S.erase(S.begin()+i);
                i--;
                continue;
            }
            x=S[i];
        }
        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.