Created
October 13, 2022 12:56
-
-
Save trafficinc/53e55d768e662a099b7a6ba174decc02 to your computer and use it in GitHub Desktop.
Leetcode - Matching brackets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isBalanced = function (input) { | |
let open = ['{', '[', '(']; | |
let close = ['}', ']', ')']; | |
let stack = []; | |
for (let i = 0; i < input.length; i++) { | |
if (open.indexOf(input[i]) >= 0) { | |
stack.push(input[i]); | |
} else { | |
if (close.indexOf(input[i]) !== open.indexOf(stack.pop())) { | |
return false; | |
} | |
} | |
} | |
if (stack.length > 0) { | |
return false; | |
} | |
return true; | |
}; | |
console.log(isBalanced("[[]]")); // true | |
console.log(isBalanced("[[]")); // false | |
console.log(isBalanced("[]]")); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment