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

for (let i = 0, n = S.length; i < n; i++) {
let c = S.charAt(i);

switch (c) {
case '{':
case '[':
case '(':
stack.push(c);
break;
case '}':
if (stack.pop() !== '{') return 0;
break;
case ']':
if (stack.pop() !== '[') return 0;
break;
case ')':
if (stack.pop() !== '(') return 0;
break;
}
}

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


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

Codility #9-PermCheck  (0) 2018.09.18
Codility #8-TapeEquilibrium  (0) 2018.09.18
Codility #7-Nesting  (0) 2018.08.18
Codility #3-PermMissingElem  (0) 2018.08.18
Codility #3-FrogJmp  (0) 2018.08.18

+ Recent posts