Maching Braces

Maching Braces

Given an expression string exp, write a program to examine whether the pairs and the orders of “{“, “}”, “(“, “)”, “[“, “]” are correct in exp. The function is expected to return a String Array YES NO
Example:
console.log(Brackets([”()[]{}”,”([{}])”, “()]]”]) [YES,YES,NO]

function checkBraces(braces) {
    if (braces.length % 2 !== 0) return false;
    
    let i = 0;
    let result = [];
    while(i < braces.length) {
        if(braces[i] === "{" || braces[i] === "(" || braces[i]=== "[") {
           result.push(braces[i]);
        } else if(braces[i] === "}" && result[result.length-1] === "{") {
            result.pop()
        } else if(braces[i] === ")" && result[result.length-1] === "(") {
            result.pop()
        } else if(braces[i] === "]" && result[result.length-1] === "[") {
            result.pop()
        } else {
            return false;
        }
        i++
    }
    
    return result.length === 0;
};

function matchingBraces(braces) {
    let result = []
    braces.forEach((b) => {
        if (checkBraces(b)) result.push('YES');
        else result.push('NO');
    });
    
    return result;
}


First we create function in our case checkBraces with only one parameter named braces, also we create a i variable that will be equal to 0 and result to []

We check if (braces.length % 2 !== 0) return false;

We do while loop while i < braces.length and we do the following conditions:

if(braces[i] === “{“ || braces[i] === “(“ || braces[i]=== “[”) { result.push(braces[i]);
We check if braces is { or ( or [ and if it is we push it into the result array.


else if(braces[i] === “}” && result[result.length-1] === “{“) { result.pop()
We check if the braces is } and previous one is { than we have result true;


else if(braces[i] === “)” && result[result.length-1] === “(“) { result.pop()
We check if the braces is ) and previous one is ( than we have result true;


else if(braces[i] === “]” && result[result.length-1] === “[”) { result.pop()
Finally we check if braces is ] and previous one is [ than yet again we have result true;


if none of these statements are true we return with else false; and increment i by 1;


At the end if all the braces are removed we check return result.length === 0;

We create another function called matchingBraces with parameter braces and variable result initialized to empty array [];

We loop the braces with forEach loop and inside we check
if (checkBraces(b)) result.push(‘YES’);
else result.push(‘NO’);


In the end we return the result

comments powered by Disqus