Java Simple and easy to understand solution, using stack, clean code with comments
PLEASE UPVOTE IF YOU LIKE THIS SOLUTION\n\n\n\nclass Solution {\n    public int longestValidParentheses(String s) {\n        Stack<Integer> stack = new Stack();
**PLEASE UPVOTE IF YOU LIKE THIS SOLUTION**\n\n\n```\nclass Solution {\n    public int longestValidParentheses(String s) {\n        Stack<Integer> stack = new Stack();\n        stack.push(-1);\n        \n        int maxLen = 0;\n        \n        for(int i = 0; i < s.length(); i++){\n            if(s.charAt(i) == \'(\'){ //opening bracket\n                \n                stack.push(i);\n            }else if(s.charAt(i) == \')\'){ //closing bracket\n                \n                stack.pop();\n                if(stack.empty()){\n                    //reset\n                    stack.push(i);\n                }else{\n                    //update maxLen\n                    maxLen = Math.max(maxLen, i - stack.peek());\n                }\n                    \n            }\n        }\n        \n        return maxLen;\n    }\n\n}\n\n```
['Stack', 'Java']