is-balanced
This commit is contained in:
parent
e0d2ed3bd0
commit
84804245dd
21
src/is-balanced.js
Normal file
21
src/is-balanced.js
Normal 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
|
Loading…
Reference in New Issue
Block a user