is-balanced

This commit is contained in:
KoroLion 2021-05-28 17:14:17 +03:00
parent e0d2ed3bd0
commit 84804245dd

21
src/is-balanced.js Normal file
View File

@ -0,0 +1,21 @@
/**
* Проверка на сбалансированность фигурных скобкок
* @param {string} input
* @return {boolean}
*/
function isBalanced(input) {
const stack = [];
for (let c of input) {
if (c === '{') {
stack.push(c);
} else if (c === '}') {
if (stack.pop() !== '{') {
return false;
}
}
}
return stack.length === 0;
}
console.log('balanced:', isBalanced('{{}{}}{}')); // true
console.log('not balanced:', isBalanced('{}{{}')); // false