BASIC IF ELSE STATEMENT ✅✅69_BEATS_100%✅✅🔥Easy Solution🔥With Explanation🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nwe need to count how many illegal paranthesis are there \na pair of paranthesis is lega
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe need to count how many illegal paranthesis are there \na pair of paranthesis is legal only if it is like this ()\nmeans ) followed by (\nso we keep track of brackets encountered in a stack and we compare legal vs illegal \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe will have a stack \nfor every bracket we will check \nif it is ) we will see if the last bracket in our stack is (\n    if yes we will pop our stack bcoz that is legal \nif not we will push onto stack as it has no partner bracket to pair up with \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![wallhaven-3lv8j6.jpg](https://assets.leetcode.com/users/images/f4d852a3-1194-4aec-9ded-e3843eeb06f8_1728433088.3672962.jpeg)\n\n# Code\n```javascript []\nvar minAddToMakeValid = function(s) {\n    let stack=[]\n    for(let x of s){\n        if(stack[stack.length-1]==\'(\' && x==\')\')stack.pop()\n            \n        else stack.push(x)\n    }\n    return stack.length\n};\n```\n```python []\ndef minAddToMakeValid(s):\n    stack = []\n    for x in s:\n        if stack and stack[-1] == \'(\' and x == \')\':\n            stack.pop()\n        else:\n            stack.append(x)\n    return len(stack)\n\n# Example usage\nprint(minAddToMakeValid("())"))\n\n```\n```java []\npublic class Solution {\n    public int minAddToMakeValid(String s) {\n        Stack<Character> stack = new Stack<>();\n        for (char x : s.toCharArray()) {\n            if (!stack.isEmpty() && stack.peek() == \'(\' && x == \')\') {\n                stack.pop();\n            } else {\n                stack.push(x);\n            }\n        }\n        return stack.size();\n    }\n\n    public static void main(String[] args) {\n        Solution sol = new Solution();\n        System.out.println(sol.minAddToMakeValid("())")); // Example usage\n    }\n}\n\n```\n```c []\n#include <stdio.h>\n#include <string.h>\n\nint minAddToMakeValid(const char* s) {\n    int top = -1;\n    int n = strlen(s);\n    char stack[n];\n    \n    for (int i = 0; i < n; i++) {\n        if (top >= 0 && stack[top] == \'(\' && s[i] == \')\') {\n            top--;  // Pop stack\n        } else {\n            stack[++top] = s[i];  // Push stack\n        }\n    }\n    \n    return top + 1;  // Stack size is top + 1\n}\n\nint main() {\n    printf("%d\\n", minAddToMakeValid("())")); // Example usage\n    return 0;\n}\n```\n```c++ []\n#include <iostream>\n#include <stack>\n#include <string>\n\nusing namespace std;\n\nint minAddToMakeValid(string s) {\n    stack<char> stack;\n    for (char x : s) {\n        if (!stack.empty() && stack.top() == \'(\' && x == \')\') {\n            stack.pop();\n        } else {\n            stack.push(x);\n        }\n    }\n    return stack.size();\n}\n\nint main() {\n    cout << minAddToMakeValid("())") << endl; // Example usage\n    return 0;\n}\n```\n\n```
['String', 'Stack', 'Greedy', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']