Check if expression contains redundant bracket or not | Set 2 | Java program to check for redundant braces in the string ; Function to check for redundant braces ; count of no of signs ; Driver Code
class GFG { static boolean IsRedundantBraces ( String A ) { int a = 0 , b = 0 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) == ' ( ' && A . charAt ( i + 2 ) == ' ) ' ) return true ; if ( A . charAt ( i ) == ' * ' || A . charAt ( i ) == ' + ' || A . charAt ( i ) == ' - ' || A . charAt ( i ) == ' / ' ) a ++ ; if ( A . charAt ( i ) == ' ( ' ) b ++ ; } if ( b > a ) return true ; return false ; } public static void main ( String [ ] args ) { String A = " ( ( ( a + b ) ▁ + ▁ c ) ▁ + ▁ d ) " ; if ( IsRedundantBraces ( A ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }