function solution(S) {
let stack = [];

for (let i = 0, n = S.length; i < n; i++) {
let c = S.charAt(i);
if (c === '(') {
stack.push(c);
} else if (c === ')') {
if (stack.length === 0) {
return 0;
} else if (stack[stack.length - 1] === '(') {
stack.pop();
}
}
}

return stack.length === 0 ? 1 : 0;
}


'IT General' 카테고리의 다른 글

Codility #8-TapeEquilibrium  (0) 2018.09.18
Codility #7-Brackets  (0) 2018.08.18
Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18
Codility #2-OddOccurrencesInArray  (0) 2018.08.18

+ Recent posts