문제 9012번
https://www.acmicpc.net/problem/9012
9012번: 괄호
괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고
www.acmicpc.net
사용 언어
javaScript
풀이
let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
const count = Number(input[0]);
function judge(ps) {
let openCount = 0;
let closeCount = 0;
for (let i in ps) {
let target = ps[i];
if (openCount === 0 && target === ')') {
return 'NO';
} else {
if (target === '(') {
openCount += 1;
} else {
closeCount += 1;
}
if (openCount === closeCount) {
openCount = 0;
closeCount = 0;
}
}
}
if (openCount === closeCount) {
return 'YES';
} else {
return 'NO';
}
}
for (i = 1; i < count + 1; i++) {
ps = input[i];
let yesOrNo = judge(ps);
console.log(yesOrNo);
}
더 나은 방법..
열리는 괄호 일때는 push, 닫히는 괄호일때는 pop으로 구현하는게 더 나았을것 같다.
'CS > Algorithm' 카테고리의 다른 글
백준 1874 (0) | 2021.05.17 |
---|---|
백준 9093 (0) | 2021.05.17 |
해시 > 전화번호 목록 (0) | 2021.04.04 |
힙 > 더 맵게 (0) | 2021.04.04 |
스택 > 다리를 지나는 트럭 (0) | 2021.03.28 |
댓글