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
def canMadeEmpty ( s , n ) : NEW_LINE INDENT st = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if s [ i ] == ' c ' : NEW_LINE INDENT if len ( st ) >= 2 : NEW_LINE INDENT b = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE a = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE if a != ' a ' or b != ' b ' : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT st . append ( s [ i ] ) NEW_LINE DEDENT DEDENT if len ( st ) == 0 : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT s = " aabcbc " NEW_LINE n = len ( s ) NEW_LINE print ( canMadeEmpty ( s , n ) ) NEW_LINE