Longest Valid Parentheses
int longestValidParentheses(string s) {
vector<int> f(s.size(),0);
int ret=0;
for(int i=s.size()-2;i>=0;i--){
int match = i+f[i+1]+1;
if(s[i]=='(' && match<s.size() && s[match]==')'){
f[i]=f[i+1]+2;
if(match+1<s.size()) f[i]+=f[match+1];
}
ret=max(ret,f[i]);
}
return ret;
}
Last updated
Was this helpful?