Created
July 4, 2019 12:00
-
-
Save kharandziuk/d1dc4c0545bb66d6f5c17685cc144f25 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function isBalanced(str) { | |
| const stack = str.split('') | |
| const compare = [] | |
| while(stack.length) { | |
| //console.log(stack, compare) | |
| let current = stack.shift(); | |
| if(compare.length == 0) { | |
| compare.push(current) | |
| } else { | |
| if(compare[compare.length - 1] === '(' && current === ')') { | |
| compare.pop() | |
| } else { | |
| compare.push(current) | |
| } | |
| } | |
| } | |
| return compare.length === 0 | |
| } | |
| console.log(isBalanced('()') === true) | |
| console.log(isBalanced('(') === false) | |
| console.log(isBalanced(')') === false) | |
| 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