Reverse substrings between each pair of parenthesis | Javascript implementation of the approach ; Function to return the modified string ; Push the index of the current opening bracket ; Reverse the substring starting after the last encountered opening bracket till the current character ; To store the modified string ; Driver code
function reverse ( A , l , h ) { if ( l < h ) { let ch = A [ l ] ; A [ l ] = A [ h ] ; A [ h ] = ch ; reverse ( A , l + 1 , h - 1 ) ; } } function reverseParentheses ( str , len ) { let st = [ ] ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) { st . push ( i ) ; } else if ( str [ i ] == ' ' ) { let A = [ ... str ] reverse ( A , st [ st . length - 1 ] + 1 , i ) ; str = [ ... A ] ; st . pop ( ) ; } } let res = " " ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] != ' ' && str [ i ] != ' ' ) { res += ( str [ i ] ) ; } } return res ; } let str = " " ; let len = str . length ; document . write ( reverseParentheses ( str , len ) ) ;