We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionisBalanced(s){// Write your code hereletopenStack=newArray();letidx=0;letvalid=true;letcorrelationMap=newMap([[')','('],[']','['],['}','{'],]);while(valid&&idx<s.length){if(correlationMap.has(s[idx])&&(correlationMap.get(s[idx])!=openStack.pop())){valid=false;}elseif(!correlationMap.has(s[idx])){openStack.push(s[idx]);}idx++;}if(openStack.length>0)valid=false;return(valid)?('YES'):('NO');}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
js answer using a map for checking correlation: