Check if a string can be emptied by removing all subsequences of the form "10" | Java program for the above approach ; Function to find if string is reducible to NULL ; Length of string ; Stack to store all 1 s ; Iterate over the characters of the string ; If current character is 1 ; Push it into the stack ; Pop from the stack ; If the stack is empty ; Driver code
import java . io . * ; import java . util . * ; class GFG { static boolean isReducible ( String str ) { int N = str . length ( ) ; ArrayList < Character > s = new ArrayList < Character > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '1' ) s . add ( str . charAt ( i ) ) ; else if ( s . size ( ) > 0 ) s . remove ( s . size ( ) - 1 ) ; else return false ; } if ( s . size ( ) == 0 ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { String str = "11011000" ; if ( isReducible ( str ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }