My easy O(n) java solution with explanation
public class Solution {\n        public int longestValidParentheses(String s) {\n            int res=0;\n            int tep=0;\n            Stack<Integer> s1=n
    public class Solution {\n        public int longestValidParentheses(String s) {\n            int res=0;\n            int tep=0;\n            Stack<Integer> s1=new Stack<>();\n            int data[]=new int[s.length()];\n            for(int i=0;i<s.length();i++){\n                char c=s.charAt(i);\n                if(c=='(') s1.push(i);\n                else{\n                    if(!s1.empty()){\n                        data[i]=1;\n                        data[s1.pop()]=1;\n                    }\n                }\n            }\n            for(int i:data){\n                if(i==1) tep++;\n                else {res=Math.max(tep,res);tep=0;}\n            }\n            return Math.max(tep,res);\n        }\n    }\n\nImaging we are coloring the original string, each substring that has valid parentheses is colored with '1' and other characters are colored by '0'.\nFor example "( ) ( ( ) "would become "11011". Thus, the problem has converted to finding the longest subsequence that all elements are '1', which could be easily solved.
['Java']