Answers for "check for balanced parentheses in javascript"

0

check for balanced parentheses in javascript

function Check()
{
    let s  = [];
    let pObj = {'{':'}','(':')','[':']'}
    let sTop = "";

    for(let i=0;i<str.length;i++)
    {
        if(str[i] == '{' || str[i] == '(' || str[i] == '[')
        {
            s.push(str[i]);
        }
        else
        {
            sTop = s.pop();
            if(str[i] !== pObj[sTop])
            {
                return 0;
            }
        }
    }
    return 1;
}

var str = userInput[0];

res = Check(str);
if(res == 0)
{
    console.log("Unbalanced");
}
else
{
    console.log("Balanced");
}
Posted by: Guest on December-08-2020

Code answers related to "check for balanced parentheses in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language