Reverse substrings between each pair of parenthesis | Function to return the modified string ; Push the index of the current opening bracket ; Reverse the substarting after the last encountered opening bracket till the current character ; To store the modified string ; Driver code
def reverseParentheses ( strr , lenn ) : NEW_LINE INDENT st = [ ] NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] == ' ( ' ) : NEW_LINE INDENT st . append ( i ) NEW_LINE DEDENT elif ( strr [ i ] == ' ) ' ) : NEW_LINE INDENT temp = strr [ st [ - 1 ] : i + 1 ] NEW_LINE strr = strr [ : st [ - 1 ] ] + temp [ : : - 1 ] + strr [ i + 1 : ] NEW_LINE del st [ - 1 ] NEW_LINE DEDENT DEDENT res = " " NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] != ' ) ' and strr [ i ] != ' ( ' ) : NEW_LINE INDENT res += ( strr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " ( skeeg ( for ) skeeg ) " NEW_LINE lenn = len ( strr ) NEW_LINE st = [ i for i in strr ] NEW_LINE print ( reverseParentheses ( strr , lenn ) ) NEW_LINE DEDENT