Created
August 7, 2017 21:12
-
-
Save JiLiZART/e6b7582952bff155ec8e30674f6a1dad 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
const isB = (str) => { | |
const strArr = str.split(''); | |
const stack = []; | |
for (let char of strArr) { | |
if (char.startsWith('[') || char.startsWith('(')) { | |
stack.push(char); | |
} else { | |
// Первый символ строки не открывающаяся скобка, значит уже не верно | |
if (stack.length === 0) { | |
return false; | |
} | |
const top = stack.pop(); | |
if ((top === '[' && char !== ']') || (top === '(' && char !== ')')) { | |
return false; | |
} | |
} | |
} | |
return stack.length === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment