Easy Java solution | 2 methods Explained with and without using Stack ✨
\n Add your space complexity here, e.g. O(n) \n\n# Code\n\nclass Solution {\n    public int minAddToMakeValid(String s) {\n      Stack<Character> stack = new St
\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minAddToMakeValid(String s) {\n      Stack<Character> stack = new Stack<>();\n      int i=0;\n      while(i<s.length()){\n          if(s.charAt(i)==\')\'){\n              if(!stack.isEmpty() && stack.peek()==\'(\'){\n                  stack.pop();\n              }\n              else{\n                  stack.push(s.charAt(i));\n              }\n          }\n            \n          else{\n              stack.push(s.charAt(i));\n                \n          }\n            \n          i++;\n      }\n      return stack.size();\n    }\n}\n\n//======================Second Approach=============================\n\nclass Solution {\n    public int minAddToMakeValid(String s) {\n        int left =0;\n        int right =0;\n        for(int i=0; i<s.length(); i++){\n            if(s.charAt(i)==\'(\'){\n                left++;\n            }\n            else{\n                if(left>0) left--;\n                else right++;\n            }\n        }\n        return left+right;\n    }\n}\n```
['String', 'Stack', 'Java']