Simple solution in Java Must read
1541. Minimum Insertions to Balance a Parentheses String\n921. Minimum Add to Make Parentheses Valid\n20. Valid Parentheses --> simple yet tricky\n301. Remove I
[1541. Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/solutions/5348641/the-parenthesis-template-question-brute-intuitive-interview/)\n[921. Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/solutions/5348632/brute-intuitive-solution-with-and-without-stack-beast-time-and-space/)\n[20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/solutions/5159895/brute-intuitive-solution-beats-98/) --> *simple yet tricky*\n[301. Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/solutions/5305306/have-fun-solving-this-easy-problem/) -> **Important**\n[32. Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/solutions/5348623/simple-solution-in-java-must-read/)\n[1614. Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/solutions/5304994/the-parenthesis-problems/)\n[282. Expression Add Operators](https://leetcode.com/problems/expression-add-operators/solutions/5348619/brute-intuitive-recursive-solution-must-read/) --> **Important**\n[2232. Minimize Result by Adding Parentheses to Expression](https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/solutions/5304516/beats-98/)\n[241. Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/solutions/5304322/java-simple-solution/)\n[856. Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/solutions/5203496/scoreofparentheses/) --> **unique**\n[678. Valid Parenthesis String]()\n[1003. Check If Word Is Valid After Substitutions]()\n[1963. Minimum Number of Swaps to Make the String Balanced]()\n\n\n# Code\n```java []\n// TC : O(n)\n// SC : O(n)\n\nclass Solution {\n    \n    public int longestValidParentheses(String s) {\n        if(s==null || s.length()<2){\n            return 0;\n        }\n\n        Stack<Integer> st =new Stack<>();\n\n        for(int i=0;i<s.length();i++){\n            if(s.charAt(i) == \'(\'){\n                st.push(i);\n            } else{\n\n                // current closing bracket\n\n                if(!st.empty() && s.charAt(st.peek()) == \'(\'){\n                    // balanced case\n                    st.pop();\n                } else {\n                    // unbalanced case\n                    st.push(i);\n                }\n            }\n        }\n\n        int maxLen = 0;\n        int endTerminal = s.length();\n\n        while(!st.empty()){\n            int startTerminal  = st.pop();\n            maxLen = Math.max(maxLen, endTerminal - startTerminal -1);\n            endTerminal = startTerminal;\n        }\n\n        return Math.max(endTerminal, maxLen);\n    }\n}\n//https://www.youtube.com/watch?v=mRsCOOGTJlM\n```
['Java']