Python3 || 10 lines, track ()s || T/S: 99% / 54%
Here\'s the intuition:\n\nFirst thoughts go right to using a stack, but we only need to determine the number the mis-balanced parenthesis pairs and return an in
Here\'s the intuition:\n\nFirst thoughts go right to using a stack, but we only need to determine the number the mis-balanced parenthesis pairs and return an integer. We can accomplish this task with simple counting.\n```python3 []\nclass Solution:\n    def minAddToMakeValid(self, s: str) -> int:\n\n        moves = tally = 0\n\n        for ch in s:\n            if   ch ==\'(\':\n                tally+= 1\n            elif ch ==\')\' and tally > 0:\n                tally-= 1\n            else:\n                tally = 0\n                moves+= 1\n                \n        return tally + moves\n```\n```cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        int moves = 0, tally = 0;\n\n        for (char ch : s) {\n            if (ch == \'(\') {\n                tally++;\n            } else if (ch == \')\' && tally > 0) {\n                tally--;\n            } else {\n                moves++;}}\n        \n        return tally + moves;}\n};\n```\n```java []\nclass Solution {\n    public int minAddToMakeValid(String s) {\n\n        int moves = 0, tally = 0;\n\n        for (char ch : s.toCharArray()) {\n            if (ch == \'(\') {\n                tally++;\n            } else if (ch == \')\' && tally > 0) {\n                tally--;\n            } else {\n                moves++;}}\n        \n        return tally + moves;}}\n```\n[https://leetcode.com/problems/sorted-gcd-pair-queries/submissions/1416439710/?submissionId=1416439117](https://leetcode.com/problems/sorted-gcd-pair-queries/submissions/1416439710/?submissionId=1416439117)\n\nI could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(1), in which *N* ~ `len(s)`.
['C++', 'Java', 'Python3']