[Java] Stack Solution || With Explanation
UPVOTE IF YOU LIKE THE EXPLANATION !!\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n    Stack Data Structure\n# Approach\n Descr
# UPVOTE IF YOU LIKE THE EXPLANATION !!\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n    Stack Data Structure\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Stack consists of **\'(\'** and **\')\'** symbols only.\n- In case of **\'(\'** you have to push it into the **Stack**.\n- In case of **\')\'** there are two conditions we have to handle.\n 1. Firstly when the stack is **empty**.\n 2. Secondly when the stack is **not empty** .\n- <h3>When Stack is empty!!</h3>\n 1. Increase the count value because we dont have enough **\'(\'** that equal to **\')\'**\n- <h3>When stack is not empty!!</h3>\n1. If stack is not empty definetly there will be a **\'(\'** at that time you can just pop it.\n- <h3>When Stack consists of only \'(\' !!</h3>\n 1. In this case we just have to add size of the **Stack** to the **count** variable.\n 2. **count** is the minimum additions to make parentheses valid.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N)\n# Code\n```\nclass Solution {\n    public int minAddToMakeValid(String s) {\n       Stack<Character> stack = new Stack<>();\n       int count = 0;\n       for(char c : s.toCharArray()) {\n           if(stack.isEmpty() ) {\n              if(c == \'(\') stack.push(c);\n              else count++;\n           }\n           else {\n               if(c==\'(\') stack.push(c);\n               else {\n                   stack.pop();\n                }\n           }\n\n       }\n       if(!stack.isEmpty() && stack.peek() == \'(\') {\n           count += stack.size();\n       }\n\n        return count;\n    }\n}\n```
['Stack', 'Java']