Check if a string can be emptied by removing all subsequences of the form "10" | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isReducible ( string str ) { int N = str . size ( ) ; stack < char > s ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '1' ) s . push ( str [ i ] ) ; else if ( ! s . empty ( ) ) s . pop ( ) ; else return false ; } return s . empty ( ) ; } int main ( ) { string str = "11011000" ; if ( isReducible ( str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }