C++ Two pointers
Intuition\nWhen the number of open parentheses equals closed, we found a primitive string.\n# Solution\nUse two pointers to track primitive strings; when open =
# Intuition\nWhen the number of ```open``` parentheses equals ```closed```, we found a primitive string.\n# Solution\nUse two pointers to track primitive strings; when ```open == close```, remove outermost parentheses and add the string to the result.\n```\nstring removeOuterParentheses(string S, string res = "") {\n  for (auto p1 = 0, p2 = 0, open = 0, close = 0; p2 < S.size(); ++p2) {\n    if (S[p2] == \'(\') ++open;\n    else ++close;\n    if (open == close) {\n      res += S.substr(p1 + 1, p2 - p1 - 1);\n      p1 = p2 + 1;\n    }\n  }\n  return res;\n}\n```\n# Complexity Analysis\nRuntime: O(n).\nMemory: O(n) to store the result.
[]