Remove “b” and “ac” from a given string

22 Jun, 2022
Given a string, eliminate all “b” and “ac” in the string, you have to replace them in-place, and you are only allowed to iterate over the string once. (Source Google Interview Question) Examples:
acbac   ==>  ""
aaac    ==>  aa
ababac  ==>   aa
bbbbd   ==>   d
The two conditions are: 1. Filtering of all ‘b’ and ‘ac’ should be in single pass 2. No extra space allowed. The approach is to use two index variables i and j. We move forward in string using ‘i’ and add characters using index j except ‘b’ and ‘ac’. The trick here is how to track ‘a’ before ‘c’. An interesting approach is to use a two state machine. The state is maintained to TWO when previous character is ‘a’, otherwise state is ONE. 1) If state is ONE, then do NOT copy the current character to output if one of the following conditions is true ...a) Current character is ‘b’ (We need to remove ‘b’) ...b) Current character is ‘a’ (Next character may be ‘c’) 2) If state is TWO and current character is not ‘c’, we first need to make sure that we copy the previous character ‘a’. Then we check the current character, if current character is not ‘b’ and not ‘a’, then we copy it to output. 
C++
Java
Python
C#
Javascript
// A C++ program to remove "b" and 'ac' from input string#include <iostream>using namespace std;#define ONE 1#define TWO 2 // The main function that removes occurrences of "a" and "bc"// in input stringvoid stringFilter(char *str){    // state is initially ONE (The previous character is not a)    int state = ONE;     // i and j are index variables, i is used to read next    // character of input string, j is used for indexes of output    // string (modified input string)    int j = 0;     // Process all characters of input string one by one    for (int i = 0; str[i] != '\0'; i++)    {        /* If state is ONE, then do NOT copy the current character          to output if one of the following conditions is true           ...a) Current character is 'b' (We need to remove 'b')           ...b) Current character is 'a' (Next character may be 'c') */        if (state == ONE && str[i] != 'a' && str[i] != 'b')        {            str[j] = str[i];            j++;        }         // If state is TWO and current character is not 'c' (other-        // wise we ignore both previous and current characters)        if (state == TWO && str[i] != 'c')        {            // First copy the previous 'a'            str[j] = 'a';            j++;             // Then copy the current character if it is not 'a'            // and 'b'            if (str[i] != 'a' && str[i] != 'b')            {                str[j] = str[i];                j++;            }        }         // Change state according to current character        state = (str[i] == 'a')? TWO: ONE;    }     // If last character was 'a', copy it to output    if (state == TWO)    {        str[j] = 'a';        j++;    }     // Set the string terminator    str[j] = '\0';} /* Driver program to check above functions */int main(){    char str1[] = "ad";    stringFilter(str1);    cout << str1 << endl;     char str2[] = "acbac";    stringFilter(str2);    cout << str2 << endl;     char str3[] = "aaac";    stringFilter(str3);    cout << str3 << endl;     char str4[] = "react";    stringFilter(str4);    cout << str4 << endl;     char str5[] = "aa";    stringFilter(str5);    cout << str5 << endl;     char str6[] = "ababaac";    stringFilter(str6);    cout << str6 << endl;     return 0;}
// A Java program to remove "b" and// 'ac' from input Stringimport java.util.*; class GFG{static final int ONE = 1;static final int TWO = 2; // The main function that removes occurrences// of "a" and "bc" in input Stringstatic char[] StringFilter(char []str){    // state is initially ONE    // (The previous character is not a)    int state = ONE;     // i and j are index variables,    // i is used to read next    // character of input String,    // j is used for indexes of output    // String (modified input String)    int j = 0;     // Process all characters of    // input String one by one    for (int i = 0; i < str.length; i++)    {        /* If state is ONE, then do NOT copy        the current character to output if        one of the following conditions is true        ...a) Current character is 'b'              (We need to remove 'b')        ...b) Current character is 'a'              (Next character may be 'c') */        if (state == ONE && str[i] != 'a' &&                            str[i] != 'b')        {            str[j] = str[i];            j++;        }         // If state is TWO and current character        // is not 'c' (otherwise we ignore both        // previous and current characters)        if (state == TWO && str[i] != 'c')        {            // First copy the previous 'a'            str[j] = 'a';            j++;             // Then copy the current character            // if it is not 'a' and 'b'            if (str[i] != 'a' && str[i] != 'b')            {                str[j] = str[i];                j++;            }        }         // Change state according to current character        state = (str[i] == 'a') ? TWO : ONE;    }     // If last character was 'a',    // copy it to output    if (state == TWO)    {        str[j] = 'a';        j++;    }    return Arrays.copyOfRange(str, 0, j);} // Driver Codepublic static void main(String[] args){    char str1[] = "ad".toCharArray();    str1 = StringFilter(str1);    System.out.print(String.valueOf(str1) + "\n");     char str2[] = "acbac".toCharArray();    str2 = StringFilter(str2);    System.out.print(String.valueOf(str2) + "\n");     char str3[] = "aaac".toCharArray();    str3 = StringFilter(str3);    System.out.print(String.valueOf(str3) + "\n");     char str4[] = "react".toCharArray();    str4 = StringFilter(str4);    System.out.print(String.valueOf(str4) + "\n");     char str5[] = "aa".toCharArray();    str5 = StringFilter(str5);    System.out.print(String.valueOf(str5) + "\n");     char str6[] = "ababaac".toCharArray();    str6 = StringFilter(str6);    System.out.print(String.valueOf(str6) + "\n");}} // This code is contributed by 29AjayKumar
# A Python program to remove "b" and 'ac' from input stringONE = 1TWO = 2 # Utility function to convert string to listdef toList(string):    l = []    for x in string:        l.append(x)    return l # Utility function to convert list to stringdef toString(l):    return ''.join(l) # The main function that removes occurrences of "a" and "bc"# in input stringdef stringFilter(string):     # state is initially ONE (The previous character is not a)    state = ONE     # i and j are index variables, i is used to read next    # character of input string, j is used for indexes of    # output string (modified input string)    j = 0     # Process all characters of input string one by one    for i in xrange(len(string)):         # If state is ONE, then do NOT copy the current character        # to output if one of the following conditions is true        # ...a) Current character is 'b' (We need to remove 'b')        # ...b) Current character is 'a' (Next character may be 'c')        if state == ONE and string[i] != 'a' and string[i] != 'b':            string[j] = string[i]            j += 1         # If state is TWO and current character is not 'c' (other-        # wise we ignore both previous and current characters)        if state == TWO and string[i] != 'c':            # First copy the previous 'a'            string[j] = 'a'            j += 1             # Then copy the current character if it is not 'a' and 'b'            if string[i] != 'a' and string[i] != 'b':                string[j] = string[i]                j += 1         # Change state according to current character         state = TWO if string[i] == 'a' else ONE     # If last character was 'a', copy it to output    if state == TWO:        string[j] = 'a'        j += 1     return toString(string[:j]) # Driver programstring1 = stringFilter(toList("ad"))print string1 string2 = stringFilter(toList("acbac"))print string2 string3 = stringFilter(toList("aaac"))print string3 string4 = stringFilter(toList("react"))print string4 string5 = stringFilter(toList("aa"))print string5 string6 = stringFilter(toList("ababaac"))print string6 # This code is contributed by BHAVYA JAIN
// A C# program to remove "b" and// 'ac' from input Stringusing System; class GFG{static readonly int ONE = 1;static readonly int TWO = 2; // The main function that removes occurrences// of "a" and "bc" in input Stringstatic char[] StringFilter(char []str){         // state is initially ONE    // (The previous character is not a)    int state = ONE;     // i and j are index variables,    // i is used to read next    // character of input String,    // j is used for indexes of output    // String (modified input String)    int j = 0;     // Process all characters of    // input String one by one    for (int i = 0; i < str.Length; i++)    {        /* If state is ONE, then do NOT copy        the current character to output if        one of the following conditions is true        ...a) Current character is 'b'            (We need to remove 'b')        ...b) Current character is 'a'            (Next character may be 'c') */        if (state == ONE && str[i] != 'a' &&                            str[i] != 'b')        {            str[j] = str[i];            j++;        }         // If state is TWO and current character        // is not 'c' (otherwise we ignore both        // previous and current characters)        if (state == TWO && str[i] != 'c')        {            // First copy the previous 'a'            str[j] = 'a';            j++;             // Then copy the current character            // if it is not 'a' and 'b'            if (str[i] != 'a' && str[i] != 'b')            {                str[j] = str[i];                j++;            }        }         // Change state according to        // current character        state = (str[i] == 'a') ? TWO : ONE;    }     // If last character was 'a',    // copy it to output    if (state == TWO)    {        str[j] = 'a';        j++;    }    return String.Join("", str).           Substring(0, j).ToCharArray();} // Driver Codepublic static void Main(String[] args){    char []str1 = "ad".ToCharArray();    str1 = StringFilter(str1);    Console.Write(String.Join("", str1) + "\n");     char []str2 = "acbac".ToCharArray();    str2 = StringFilter(str2);    Console.Write(String.Join("", str2) + "\n");     char []str3 = "aaac".ToCharArray();    str3 = StringFilter(str3);    Console.Write(String.Join("", str3) + "\n");     char []str4 = "react".ToCharArray();    str4 = StringFilter(str4);    Console.Write(String.Join("", str4) + "\n");     char []str5 = "aa".ToCharArray();    str5 = StringFilter(str5);    Console.Write(String.Join("", str5) + "\n");     char []str6 = "ababaac".ToCharArray();    str6 = StringFilter(str6);    Console.Write(String.Join("", str6) + "\n");}} // This code is contributed by Rajput-Ji
<script> // A JavaScript program to remove "b" && 'ac' from input stringconst ONE = 1const TWO = 2 // Utility function to convert string to listfunction toList(string){    let l = []    for(let x of string)        l.push(x)    return l} // Utility function to convert list to stringfunction toString(l){    return l.join('')} // The main function that removes occurrences of "a" && "bc"// in input stringfunction stringFilter(string){     // state is initially ONE (The previous character is not a)    let state = ONE     // i && j are index variables, i is used to read next    // character of input string, j is used for indexes of    // output string (modified input string)    let j = 0     // Process all characters of input string one by one    for(let i=0;i<string.length;i++){         // If state is ONE, then do NOT copy the current character        // to output if one of the following conditions is true        // ...a) Current character is 'b' (We need to remove 'b')        // ...b) Current character is 'a' (Next character may be 'c')        if(state == ONE && string[i] != 'a' && string[i] != 'b'){            string[j] = string[i]            j += 1        }         // If state is TWO && current character is not 'c' (other-        // wise we ignore both previous && current characters)        if(state == TWO && string[i] != 'c'){            // First copy the previous 'a'            string[j] = 'a'            j += 1             // Then copy the current character if it is not 'a' && 'b'            if(string[i] != 'a' && string[i] != 'b'){                string[j] = string[i]                j += 1            }        }         // Change state according to current character        state = string[i] == 'a'? TWO : ONE    }     // If last character was 'a', copy it to output    if(state == TWO){        string[j] = 'a'        j += 1    }     return toString(string.slice(0,j))} // Driver programlet string1 = stringFilter(toList("ad"))document.write(string1,"</br>") let string2 = stringFilter(toList("acbac"))document.write(string2,"</br>") let string3 = stringFilter(toList("aaac"))document.write(string3,"</br>") let string4 = stringFilter(toList("react"))document.write(string4,"</br>") let string5 = stringFilter(toList("aa"))document.write(string5,"</br>") let string6 = stringFilter(toList("ababaac"))document.write(string6,"</br>") // This code is contributed by shinjanpatra </script>
Output:
ad

aa
ret
aa
aaa
Time Complexity  :  O(n) where n  is the length of string str.
An extension of above problem where we don’t want “ac” in output at all: The above code looks fine and seems to handle all cases, but what if input string is “aacacc”, the above code produces output as “ac” which looks correct as it removes consecutive occurrences of ‘a’ and ‘c’. What if the requirement is to not have an “ac” in output string at all. Can we modify the above program to produce output as empty string for input “aacacc” and produce output as “d” when input is “abcaaccd”? It turns out that it can also be done with given restrictions. The idea is simple. We need to add following lines inside for loop of the above program. 
C
if (j>1 && str[j-2] == 'a' && str[j-1] =='c')  j = j-2;
Output:
Input => ad
Output => ad

Input => acbac
Output =>

Input => aaac
Output => aa

Input => react
Output => ret

Input => aa
Output => aa

Input => ababaac
Output => aaa

Input => abc
Output =>
Thanks to Gaurav Ahirwar for suggesting this simpler solution. This article is contributed by Varun Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
prerna saini
princiraj1992
imSaiful
nidhi_biet
29AjayKumar
Rajput-Ji
shinjanpatra
akashish__
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.