Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 02 Jun, 2021

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++




// 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;
}

Java




//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

Python3




# 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

C#




// 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

Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to get the resultant String after
// removing the required occurrences
function removeOccurrences(str)
{
 
    // String to store the resultant String
    var res = "";
    for (var 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
var str = "abcddabcddddabbbaaaaaa";
document.write(removeOccurrences(str));
 
 
// This code contributed by Princi Singh
 
</script>

Output: 

acddacdddda

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!