DEV Community

Urfan Guliyev
Urfan Guliyev

Posted on • Updated on


Leetcode - Valid Parentheses (with JavaScript)

Today I am going to show how to solve the Leetcode Valid Parentheses algorithm problem.

Here is the problem:

Alt Text


var isValid = function(s){

    const stack = []

    for (i=0; i < s.length; i++){

        let curChar = s[i];

        switch(curChar) {
            case '(': stack.push(')');
            case '[': stack.push(']');
            case '{': stack.push('}')
                topElement = stack.pop()
                if (curChar !== topElement) return false;       
    return stack.length === 0;

As I iterate through the string character by character, I check to see if any of its characters is an open or closing parenthesis.

If a character is an opening parentheses, I will push it onto the stack.

Opening parenthesis: {, [, (

If a character is a closing parentheses, I’ll pop the element on top of the stack and check if it matches the character in the current index. If the characters do not match, then the expression is invalid.

Closing parenthesis: }, ], )

Essentially, if there are still elements in the stack, then the expression is invalid.

Top comments (0)