UVA – 673 – Parentheses Balance

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		int N = Integer.parseInt(bf.readLine());
		while (N-- != 0) {
			boolean bad = false;
			char[] cs = bf.readLine().toCharArray();
			Stack<Character> stk = new Stack<Character>();
			for (char c : cs) {
				if (c == '[' || c == '(') {
					stk.push(c);
				} else {
					if (c == ']') {
						if (stk.isEmpty() || stk.peek() == '(')		bad |= true;
					} else if (c == ')') {
						if (stk.isEmpty() || stk.peek() == '[')		bad |= true;
					}
					if (!stk.isEmpty())		stk.pop();
				}
			}
			if (!stk.isEmpty())		bad = true;
			System.out.println(bad ? "No" : "Yes");
		}
	}
}

Advertisements

One thought on “UVA – 673 – Parentheses Balance

  1. Hossain says:

    i solved the probleb in the following way. but i have git runtime error . can you please help me regarding this . here is the full code
    ///////////////
    package second;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    import java.util.Stack;

    public class Paran {

    public static void main(String[] args) {

    int n;

    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    Stack firstP;
    // Stack secondP;

    while(n>0){
    firstP = new Stack();
    // secondP = new Stack();
    String series=””;

    try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    series = reader.readLine();
    }
    catch(Exception ex){}

    String result=”No”;
    //series=sc.next();
    series=series.replaceAll(“\\s+”, “”);

    //System.out.println(series);

    for(int i=0;i<series.length();i++){

    if(series.charAt(i)=='('){
    firstP.push('(');
    result="Yes";
    }

    else if(series.charAt(i)=='['){
    firstP.push('[');
    result="Yes";
    }

    else if(series.charAt(i)==')'){

    if(firstP.isEmpty()){
    result="No";
    break;

    }
    else if(firstP.peek()=='('){

    firstP.pop();
    }

    else if(firstP.peek()=='['){

    result="No";
    break;
    }

    }

    else if(series.charAt(i)==']'){

    if(firstP.isEmpty()){
    result="No";
    break;

    }
    else if(firstP.lastElement()=='['){

    firstP.pop();
    }

    else if(firstP.lastElement()=='('){

    result="No";
    break;
    }

    }

    }

    if(result.equals("Yes")){

    if(firstP.isEmpty()){
    System.out.println("Yes");

    }

    else{
    System.out.println("No");
    }

    }

    else{
    System.out.println("No");
    }
    firstP.clear();

    n–;
    }
    }

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: