text: Reduce string by removing outermost parenthesis from each primitive substring | C ++ program to implement the above approach ; 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: #include <bits/stdc++.h> NEW_LINE using namespace std ; string removeOuterParentheses ( string S ) { string res ; int count = 0 ; for ( char c : S ) { if ( c == ' ( ' && count ++ > 0 ) res += c ; if ( c == ' ) ' && count -- > 1 ) res += c ; } return res ; } int main ( ) { string S = " ( ( ) ( ) ) ( ( ) ) ( ) " ; cout << removeOuterParentheses ( S ) ; }