Remove all continuous occurrences of ‘a’ and all occurrences of ‘b’

Given a string str, the task is to remove all the continuous occurrences of a and all occurrences of b and print the resultant string.

Examples:

Input: str = “abcddabcddddabbbaaaaaa”
Output: acddacdddda
‘abcddabcddddabbbaaaaaa’ will not result in ‘acddacddddaa’ because after removing the required occurrences, the string will become ‘acddacddddaa’ which will result in ‘acddacdddda’

Input: str = “aacbccdbsssaba”
Output: acccdsssa



Approach: We initialize an empty result string. We traverse the input string if the current character is b or current character is a and last character of result string is also a then ignore the character else push the character into the result string.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to get the resultant string after
// removing the required occurrences
string removeOccurrences(string str)
{
  
    // String to store the resultant string
    string res = "";
    for (int i = 0; i < str.size(); i++) {
  
        // If 'a' appeared more than once continuously
        if (str[i] == 'a' && res.back() == 'a')
  
            // Ignore the character
            continue;
  
        // Ignore all 'b' characters
        else if (str[i] == 'b')
            continue;
  
        // Characters that will be included
        // in the resultant string
        res = res + str[i];
    }
    return res;
}
  
// Driver code
int main()
{
    string str = "abcddabcddddabbbaaaaaa";
    cout << removeOccurrences(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java implementation of the approach 
class solution
{
// Function to get the resultant String after 
// removing the required occurrences 
static String removeOccurrences(String str) 
  
    // String to store the resultant String 
    String res = ""
    for (int i = 0; i < str.length(); i++) { 
  
        // If 'a' appeared more than once continuously 
        if (str.charAt(i) == 'a' && (res.length()==0?' ':res.charAt(res.length()-1)) == 'a'
  
            // Ignore the character 
            continue
  
        // Ignore all 'b' characters 
        else if (str.charAt(i) == 'b'
            continue
  
        // Characters that will be included 
        // in the resultant String 
        res = res + str.charAt(i); 
    
    return res; 
  
// Driver code 
public static void main(String args[]) 
    String str = "abcddabcddddabbbaaaaaa"
    System.out.println(removeOccurrences(str));
}
//contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to get the resultant string 
# after removing the required occurrences 
def removeOccurrences(str) :
  
    # String to store the resultant string 
    res = ""
    for i in range(len(str)) :
          
        # If 'a' appeared more than 
        # once continuously 
        if (res) :
              
            if (str[i] == 'a' and res[-1] == 'a') :
  
                # Ignore the character 
                continue
              
            # Ignore all 'b' characters 
            elif (str[i] == 'b') :
                continue
              
            else :
                # Characters that will be included 
                # in the resultant string 
                res += str[i] 
          
        else :
              
            if (str[i] == 'a' ) :
                res += str[i]
                  
            # Ignore all 'b' characters 
            elif (str[i] == 'b') :
                continue
              
            else :
                # Characters that will be included 
                # in the resultant string 
                res += str[i] 
  
    return res
  
# Driver code 
if __name__ == "__main__"
  
    str = "abcddabcddddabbbaaaaaa"
    print(removeOccurrences(str))
      
# This code is contributed by Ryuga 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
      
// Function to get the resultant String after 
// removing the required occurrences 
static String removeOccurrences(String str) 
  
    // String to store the resultant String 
    String res = ""
    for (int i = 0; i < str.Length; i++) 
    
  
        // If 'a' appeared more than once continuously 
        if (str[i] == 'a' && (res.Length==0?' ':
                        res[res.Length-1]) == 'a'
  
            // Ignore the character 
            continue
  
        // Ignore all 'b' characters 
        else if (str[i] == 'b'
            continue
  
        // Characters that will be included 
        // in the resultant String 
        res = res + str[i]; 
    
    return res; 
  
// Driver code 
public static void Main(String []args) 
    String str = "abcddabcddddabbbaaaaaa"
    Console.WriteLine(removeOccurrences(str));
  
// This code has been contributed by 29AjayKumar

chevron_right


Output:

acddacdddda


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Ryuga, andrew1234, 29AjayKumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.