You are viewing a single comment's thread. Return to all comments →
Not the best solution in JS but meh.
const bracketsArr = s.split(''); const findClosingBracket = (arr) => { let a = true; while (a) { const closedBracket = arr.findIndex(x => x === ')' || x === '}' || x === ']'); if ((arr[closedBracket] === ")" && arr[closedBracket - 1] === '(') || (arr[closedBracket] === "]" && arr[closedBracket - 1] === '[') || (arr[closedBracket] === "}" && arr[closedBracket - 1] === '{') ) { arr.splice(closedBracket - 1, 2); } else { a = false; } if (arr.length === 0) return a; } return a; } const a = findClosingBracket(bracketsArr); return a ? "YES" : "NO";
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 →
Not the best solution in JS but meh.