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++
// 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 |
acddacdddda
Recommended Posts:
- Pattern Occurrences : Stack Implementation Java
- Maximum number of continuous Automorphic numbers
- Remove repeated digits in a given number
- Remove brackets from an algebraic string containing + and - operators
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Reduce the string to minimum length with the given operation
- Sort the given stack elements based on their modulo with K
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Check if any K ranges overlap at any point
- Stack in Python
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Convert ternary expression to Binary Tree using Stack
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Python | Stack using Doubly Linked List
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.



