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: using System ; class GFG { static string removeOuterParentheses ( string S ) { string res = " " ; int count = 0 ; for ( int c = 0 ; c < S . Length ; c ++ ) { if ( S == ' ( ' && count ++ > 0 ) res += S ; if ( S == ' ) ' && count -- > 1 ) res += S ; } return res ; } public static void Main ( ) { string S = " ( ( ) ( ) ) ( ( ) ) ( ) " ; Console . Write ( removeOuterParentheses ( S ) ) ; } }