100% beats, easy-peasy code.
\n#### I don\'t know why this problem is tagged as Medium. Easy-peasy code in O(N) time complexity and O(1) space complexity.\n\n\n# Code\n\nclass Solution {\np
\n#### I don\'t know why this problem is tagged as Medium. Easy-peasy code in O(N) time complexity and O(1) space complexity.\n\n\n# Code\n```\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        int open = 0, close = 0;\n\n        for(char ch: s){\n            if(ch == \'(\') open++;\n            else if(open == 0) close++;\n            else open--;\n        }\n\n        return (open + close);\n    }\n};\n```\n\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![leetcodeSolution.png](https://assets.leetcode.com/users/images/e4815eaf-990a-4846-8c2d-571dfa847667_1720197753.3220077.png)\n\n\n### Intuition for the Solution:\n\n1. **Tracking Unmatched Parentheses**: The code uses two counters, `open` to track unmatched opening parentheses \'(\' and `close` to track unmatched closing parentheses \')\'.\n2. **Traversing the String**: As it iterates through each character in the string:\n   - If it encounters an opening parenthesis \'(\', it increments the `open` counter.\n   - If it encounters a closing parenthesis \')\', it checks if there are unmatched opening parentheses (`open` > 0). If there are, it decrements the `open` counter to match the current closing parenthesis. If there are no unmatched opening parentheses, it increments the `close` counter.\n3. **Result**: The total number of unmatched opening and closing parentheses (`open + close`) gives the minimum number of moves required to make the string valid. This is because each unmatched parenthesis needs to be matched by inserting the corresponding opposite parenthesis.\n\n### Explanation of the Example:\nFor the input "()))":\n- The first character is \'(\', so `open` is incremented to 1.\n- The next character is \')\', so `open` is decremented to 0 (matching the \'(\').\n- The next character is \')\', so `close` is incremented to 1 (no unmatched \'(\' to match it).\n- The last character is \')\', so `close` is incremented to 2 (no unmatched \'(\' to match it).\n\nThe total moves required are `open + close = 0 + 2 = 2`.\n\nThus, the minimum number of moves required to make the string valid is 2.\n
['String', 'Greedy', 'C++']