Given a string containing just the characters ’(’ and ’)’, find the length of the longest valid (wellformed) parentheses substring.
For ”(()”, the longest valid parentheses substring is ”()”, which has length = 2.
Another example is ”)()())”, where the longest valid parentheses substring is ”()()”, which has length = 4.
public static int solution_4_1_2(char[] chars){
int start=0;
int[] st=new int[chars.length];
int top=0;
int max=0;
for(int i=0;i<chars.length;i++){
if(chars[i]=='(')
st[top++]=i;
if(chars[i]==')'){
if(top==0){
start=i+1;
}
else{
// int t=--top;
if(top==0){
max=max>=(i-start+1)?max:i-start+1;
}
else{
max=max>=(i-st[top-1])?max:i-st[top-1];
}
}
}
}
return max;
}