Skip to content

Instantly share code, notes, and snippets.

@JiLiZART
Created August 7, 2017 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JiLiZART/e6b7582952bff155ec8e30674f6a1dad to your computer and use it in GitHub Desktop.
Save JiLiZART/e6b7582952bff155ec8e30674f6a1dad to your computer and use it in GitHub Desktop.
Проверка валидности скобок ( и [
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