728x90
class Main {
static Boolean isVPS(String s){
Stack<Character>st = new Stack<>();
if(s.length()>0) {
st.push(s.charAt(0));
}
for(int idx =1 ; idx<s.length();idx++ ){
if(st.isEmpty()){st.push(s.charAt(idx));continue;}
Character top = st.peek();
char ch = s.charAt(idx);
if(ch==')'){
if(top=='('){
st.pop();
}
else{
st.push(ch);
}
}
else{
st.push(ch);
}
}
return st.isEmpty();
}
public static void main(String[] args) throws IOException {
// int n;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// StringTokenizer st = new StringTokenizer(br.readLine());
int n= Integer.parseInt(br.readLine());
// String[] inputList = new String[n];
String line = "";
for (int i = 1; (line =br.readLine())!=null; i++) {
if (!isVPS(line)) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
br.close();
}
}
'백준 > 스택' 카테고리의 다른 글
후위표기식 - 1918번(java) (0) | 2021.08.15 |
---|---|
탑-2493번(java) (0) | 2021.08.05 |
괄호제거-2800번(java) (0) | 2021.08.05 |