Remove " b " and " ac " from a given string | A Java program to remove " b " and ' ac ' from input string ; The main function that removes occurrences of " a " and " bc " in input string ; previous character ; current character ; 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 to check above functions
class GfG { static void stringFilter ( char str [ ] ) { int n = str . length ; int i = - 1 ; int j = 0 ; while ( j < n ) { if ( j < n - 1 && str [ j ] == ' a ' && str [ j + 1 ] == ' c ' ) j += 2 ; else if ( str [ j ] == ' b ' ) j ++ ; else if ( i >= 0 && str [ j ] == ' c ' && str [ i ] == ' a ' ) { i -- ; j ++ ; } else str [ ++ i ] = str [ j ++ ] ; } System . out . println ( new String ( str ) . substring ( 0 , i + 1 ) ) ; } public static void main ( String [ ] args ) { String str1 = " ad " ; stringFilter ( str1 . toCharArray ( ) ) ; String str2 = " acbac " ; stringFilter ( str2 . toCharArray ( ) ) ; String str3 = " aaac " ; stringFilter ( str3 . toCharArray ( ) ) ; String str4 = " react " ; stringFilter ( str4 . toCharArray ( ) ) ; String str5 = " aa " ; stringFilter ( str5 . toCharArray ( ) ) ; String str6 = " ababaac " ; stringFilter ( str6 . toCharArray ( ) ) ; } }