Problem

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

Example 

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.

JavaScript Code

function longestValidParentheses(s) {
    var stack = [];
    var result = 0;
 
    for(var i=0; i<=s.length-1; i++){
        var c = s.charAt(i);
        if(c=='('){
            var a = [i,0];
            stack.push(a);
        }else{
            if(stack.length == 0||stack[stack.length - 1][1]==1){
                var a = [i,1];
                stack.push(a);
            }else{
                var temp = stack.pop();
                var currentLen=0;
                if(stack.length == 0){
                    currentLen = i+1;
                }else{
                    currentLen = i-stack[stack.length - 1][0];
                }
                result = Math.max(result, currentLen);
            }
        }
    }
 
    return result;
}

0 comments:

Blogroll

Follow this blog by Email

Popular Posts