Greedy Handling of Unbalanced Parentheses | Java | C++ | Video Solution
Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/aHp2Q6y0_Nw\n# Code\njava []\nclass Solution {\n    public int minA
# Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/aHp2Q6y0_Nw\n# Code\n```java []\nclass Solution {\n    public int minAddToMakeValid(String s) {\n       int openReq = 0;\n       int openCnt = 0;\n       for(var chr : s.toCharArray()){\n        if(chr == \'(\')openCnt++;\n        else{\n            if(openCnt > 0)openCnt--;\n            else openReq++;\n        }\n       }\n       int closeReq = openCnt;\n       return openReq + closeReq;\n    }\n}\n```\n```cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n       int openReq = 0;\n       int openCnt = 0;\n       for(auto & chr : s){\n        if(chr == \'(\')openCnt++;\n        else{\n            if(openCnt > 0)openCnt--;\n            else openReq++;\n        }\n       }\n       int closeReq = openCnt;\n       return openReq + closeReq;\n    }\n};\n```
['String', 'Stack', 'Greedy', 'C++', 'Java']