const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
// const input = [6, '(())())', '(((()())()', '(()())((()))', '((()()(()))(((())))()', '()()()()(()()())()', '(()((())()('];
for(let i=1; i<input.length ; i++) {
const str = input[i];
let count = 0;
for(let j=0; j<str.length; j++) {
if(str[j] === '(') count++;
else count--;
if(count < 0) {
console.log('NO');
break;
}
}
count === 0 && console.log('YES');
count > 0 && console.log('NO');
}
const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
// const input = [6, '(())())', '(((()())()', '(()())((()))', '((()()(()))(((())))()', '()()()()(()()())()', '(()((())()('];
const result = [];
for(let i=1; i<input.length ; i++) {
const str = input[i];
let count = 0;
for(let j=0; j<str.length; j++) {
if(str[j] === '(') count++;
else count--;
if(count < 0) {
result.push('NO');
break;
}
}
count === 0 && result.push('YES');
count > 0 && result.push('NO');
}
console.log(result.join('\n'));
for 안에서 console.log 자주 사용하면 안 좋다고 하여 성장하는 마음으로 push로 변경 후 마지막에 console.log 실행
오히려 12ms나 더 많이 나왔다...어쩌라는 거지?
이유를 아는 분들은 댓글 남겨주세요!!
선물로 수학의 정석 수학I 을 드립니다.