Remove " b " and " ac " from a given string | Utility function to convert string to list ; Utility function to convert list to string ; length of string ; Check if current and next character forms ac ; If current character is b ; if current char is ' c ▁ & & ▁ last ▁ char ▁ in ▁ output ▁ ▁ is ▁ ' a ' so delete both ; Else copy curr char to output string ; Driver program
def toList ( string ) : NEW_LINE INDENT l = [ ] NEW_LINE for x in string : NEW_LINE INDENT l . append ( x ) NEW_LINE DEDENT return l NEW_LINE DEDENT def toString ( l ) : NEW_LINE INDENT return ' ' . join ( l ) NEW_LINE DEDENT def stringFilter ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE i = - 1 NEW_LINE j = 0 NEW_LINE while j < n : NEW_LINE INDENT if j < n - 1 and string [ j ] == ' a ' and string [ j + 1 ] == ' c ' : NEW_LINE INDENT j += 2 NEW_LINE DEDENT elif string [ j ] == ' b ' : NEW_LINE INDENT j += 1 NEW_LINE DEDENT elif i >= 0 and string [ j ] == ' c ' and string [ i ] == ' a ' : NEW_LINE INDENT i -= 1 NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE string [ i ] = string [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE return toString ( string [ : i ] ) NEW_LINE DEDENT string1 = " ad " NEW_LINE print " Input ▁ = > ▁ " + string1 +   " NEW_LINE Output = >   " , NEW_LINE print stringFilter ( toList ( string1 ) ) +   " NEW_LINE " NEW_LINE string2 = " acbac " NEW_LINE print " Input ▁ = > ▁ " + string2 +   " NEW_LINE Output = >   " , NEW_LINE print stringFilter ( toList ( string2 ) ) +   " NEW_LINE " NEW_LINE string3 = " aaac " NEW_LINE print " Input ▁ = > ▁ " + string3 +   " NEW_LINE Output = >   " , NEW_LINE print stringFilter ( toList ( string3 ) ) +   " NEW_LINE " NEW_LINE string4 = " react " NEW_LINE print " Input ▁ = > ▁ " + string4 +   " NEW_LINE Output = >   " , NEW_LINE print stringFilter ( toList ( string4 ) ) +   " NEW_LINE " NEW_LINE string5 = " aa " NEW_LINE print " Input ▁ = > ▁ " + string5 +   " NEW_LINE Output = >   " , NEW_LINE print stringFilter ( toList ( string5 ) ) +   " NEW_LINE " NEW_LINE string6 = " ababaac " NEW_LINE print " Input ▁ = > ▁ " + string6 +   " NEW_LINE Output = >   " , NEW_LINE print stringFilter ( toList ( string6 ) ) +   " NEW_LINE " NEW_LINE string7 = " abc " NEW_LINE print " Input ▁ = > ▁ " + string7 +   " NEW_LINE Output = >   " , NEW_LINE print stringFilter ( toList ( string7 ) ) +   " NEW_LINE " NEW_LINE