Check if a string consisting only of a , b , c can be made empty by removing substring " abc " recursively | Function to check if the given string S can be made empty ; Stores the characters of the string S ; Traverse the given string ; If the character is c ; If stack size is greater than 2 ; Pop from the stack ; Top two characters in the stack should be ' b ' and ' a ' respectively ; Otherwise , print No ; If character is ' a ' or ' b ' push to stack ; If stack is empty , then print Yes . Otherwise print No ; Driver Code
function canMadeEmpty ( s , n ) { let St = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { if ( St . length >= 2 ) { let b = St [ St . length - 1 ] ; St . pop ( ) ; let a = St [ St . length - 1 ] ; St . pop ( ) ; if ( a != ' ' b != ' ' ) return " " ; } else return " " ; } else St . push ( s [ i ] ) ; } if ( St . length == 0 ) { return " " ; } else { return " " ; } } let S = " " ; let N = S . length ; document . write ( canMadeEmpty ( S , N ) ) ;