somdaa / algorithmstudy

0 stars 0 forks source link

week-02. Stack-Brackets #3

Open levi-yo opened 6 years ago

levi-yo commented 6 years ago

https://www.acmicpc.net/problem/9012

levi-yo commented 6 years ago

import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; import java.util.Stack;

public class Bracket { static String balance(String str){ Stack s=new Stack(); String str1="YES"; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='('){ s.push(str.charAt(i)); }else if(str.charAt(i)==')' && !s.isEmpty() && s.peek().equals('(')){ s.pop(); }else if(s.isEmpty() && str.charAt(i)==')'){ str1="NO"; } } if(!s.isEmpty()) str1="NO"; return str1; } public static void main(String[] args) { // TODO Auto-generated method stub ArrayList list=new ArrayList(); Scanner sc=new Scanner(System.in); int n=Integer.parseInt(sc.nextLine()); for(int i=0;i<n;i++){ String str=sc.nextLine(); list.add(balance(str)); } Iterator it=list.iterator(); while(it.hasNext()) System.out.println(it.next()); } }

ewqsaz123 commented 6 years ago

import java.util.ArrayList; import java.util.Scanner; import java.util.Stack;

public class Main { /*

}