✅Efficient Easy to understand🔥Beginner Friendly🔥|| Stack||
Problem Understanding\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the minimum number of parentheses needed to be
# Problem Understanding\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the minimum number of parentheses needed to be added to make a given string of parentheses valid. \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Stack-based Validation**: The code uses a stack to track unmatched opening parentheses \'(\'. For each character in the string:\n    - If the character is \'(\', it is pushed onto the stack (indicating an unmatched opening parenthesis).\n    - If the character is \')\':\n        - If the stack is not empty (there is an unmatched \'(\'), it pops from the stack to indicate a match.\n        - If the stack is empty (there is no unmatched \'(\'), it increments the ans counter to account for an unmatched closing parenthesis.\n1. **Final Step**:\n    - At the end, the sum of len(stack) (which counts the unmatched opening parentheses) and ans (which counts the unmatched closing parentheses) is returned as the minimum number of parentheses that need to be added.\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def minAddToMakeValid(self, s: str) -> int:\n        stack = []\n        ans = 0\n        for i in s:\n            if i == "(":\n                stack.append(i)\n            elif len(stack)>0 and i == ")":\n                stack.pop()\n            elif i == ")":\n                ans += 1\n        return len(stack) + ans       \n```\n# SMALL REQUEST : If you found this post even remotely helpful, be kind enough to smash a upvote. I will be grateful.I will be motivated\uD83D\uDE0A\uD83D\uDE0A
['Stack', 'Python3']