C++ | Faster than 100% | Easy
A simple iterative program that iterates on the string.\nWe use a variable flag to cound the number of left parenthesis. Since we just have to remove one parent
A simple iterative program that iterates on the string.\nWe use a variable flag to cound the number of left parenthesis. Since we just have to remove one parenthesis only, we check if the flag value is greater than 1, and then add characters to the resultant string.\n\n```\nConsider the following test case\n\n\tS = "(()())(())"\n\t\t\n\t\t\t1. S[i] = "("  , flag = 1, result = "";\n\t\t\t2. S[i] = "("  , flag = 2, result = "(";\n\t\t\t3. S[i] = ")"  , flag = 1, result = "()";\n\t\t\t4. S[i] = "("  , flag = 2, result = "()(";\n\t\t\t5. S[i] = ")"  , flag = 1, result = "()()";\n\t\t\t6. S[i] = ")"  , flag = 0, result = "()()";\n\t\t\t7. S[i] = "("  , flag = 1, result = "()()";\n\t\t\t8. S[i] = "("  , flag = 2, result = "()()(";\n\t\t\t9. S[i] = ")"  , flag = 1, result = "()()()";\n\t\t\t10. S[i] = ")"  , flag = 0, result = "()()()";\n\t\t\t\n\t\t\tresult = "()()()";\n\t\t\treturn result;\n```\n\n```\n\nclass Solution {\npublic:\n    string removeOuterParentheses(string S) {\n        int flag = 0,l = int(S.length());\n        string result;\n        for(int i = 0;i < l;i++){\n            if(S[i] == \'(\'){\n                flag += 1;\n                result += flag > 1 ? "(" : "";\n            } else {\n                flag -= 1;\n                result += flag > 0 ? ")" : "";\n            }\n        }\n        return result;\n    }\n};\n\n```\n\n![image](https://assets.leetcode.com/users/images/66145b21-665b-4402-ae74-2c6565dceefe_1606804144.1577873.png)\n
['C', 'C++']