20250705 baekjoon

This commit is contained in:
songyc macbook 2025-07-05 19:03:18 +09:00
parent 8f8e0ca19a
commit 9d16f1b6b2
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,23 @@
while True:
stack = []
line = input()
if line=="." :
break
isBalance = True
for c in line:
if c == "(":
stack.append(c)
elif c== ")":
if not stack or stack[-1] != "(":
isBalance = False
break
stack.pop()
elif c == "[":
stack.append(c)
elif c== "]":
if not stack or stack[-1] != "[":
isBalance = False
break
stack.pop()
print("yes" if isBalance and not stack else "no")

View File

@ -0,0 +1,24 @@
export {};
const lines = require("fs").readFileSync(0,"utf8").toString().trim().split('\n');
const isEmpty = (arr:string[]) => arr.length === 0;
const lastValue = (arr:string[]) => arr[arr.length-1];
const n = Number(lines[0]);
for(let i=1; i<=n; i++){
let stack:string[] = [];
let flag:boolean = true;
let line = lines[i];
for(let ch of line) {
if(ch === "(") {
stack.push(ch);
}
else if(ch === ")") {
if(isEmpty(stack) || lastValue(stack) !== "(") {
flag = false;
break;
}
stack.pop();
}
}
console.log((isEmpty(stack) && flag) ? "YES" : "NO");
}