text: Reduce string by removing outermost parenthesis from each primitive substring | Function to remove the outermost parentheses of every primitive substring from the given string ; Stores the resultant string ; Stores the count of opened parentheses ; Traverse the string ; If opening parenthesis is encountered and their count exceeds 0 ; Include the character ; If closing parenthesis is encountered and their count is less than count of opening parentheses ; Include the character ; Return the resultant string ; Driver Code
code: function removeOuterParentheses ( S ) { let res = " " ; let count = 0 ; for ( let c = 0 ; c < S . length ; c ++ ) { if ( S . charAt ( c ) == ' ' && count ++ > 0 ) res += S . charAt ( c ) ; if ( S . charAt ( c ) == ' ' && count -- > 1 ) res += S . charAt ( c ) ; } return res ; } let S = " " ; document . write ( removeOuterParentheses ( S ) ) ;