반응형
https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
Brackets coding task - Learn to Code - Codility
Determine whether a given string of parentheses (multiple types) is properly nested.
app.codility.com
문제:
주어진 중첩괄호 문자열이 쌍을 이루고 있다면 1, 아니면 0 반환한다. 문자열이 비어있어도 1 반환한다.
내 풀이:
function solution(S) {
if (S.length === 0) return 1;
const stack = [];
const arr = [...S];
for (let i = 0; i < arr.length; i++) {
if ((stack[stack.length - 1] === '{' && arr[i] === '}')
|| (stack[stack.length - 1] === '[' && arr[i] === ']')
|| (stack[stack.length - 1] === '(' && arr[i] === ')')) {
stack.pop();
} else {
stack.push(arr[i]);
}
}
return Number(stack.length === 0);
}
- 채점결과: 100%
반응형
'알고리즘 > Codility' 카테고리의 다른 글
[Codility] Dominator (JavaScript) (1) | 2025.06.14 |
---|---|
[Codility] Fishes (JavaScript) (0) | 2025.06.14 |
[Codility] GenomicRangeQuery (JavaScript) (0) | 2025.06.14 |
[Codility] Count Divs (JavaScript) (0) | 2025.06.14 |
[Codility] Passing Cars (JavaScript) (0) | 2025.06.14 |
댓글