Brackets

Determine whether a given string of parentheses (multiple types) is properly nested.


Solution:

Solution to Codility's Brackets problem which is from the Codility Lesson 7: Stacks and Queues and, is solved in Java 8 with 100% performance and correctness scores. The goal here is to determine whether a given string of parentheses (multiple types) is properly nested. You can find the question of this Brackets problem in the Codility website.


import java.util.*;

public class Brackets {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String in2 = "{[()()]}";
		//String in2 = "([)()]";
		int result = solution(in2);
		System.out.println(result);
	}

    public static int solution(String S) {
        // write your code in Java SE 8
        Stack <Character> stack = new Stack<Character>();

       for(int i=0; i<S.length(); i++){
           char c = S.charAt(i);
           if (c == '{' || c == '[' || c == '('){
               stack.push(c);
           }else if(stack.size()>0){
           if(c ==')'){
               if(stack.pop() != '(')    
                   return 0;
           }
           if(c ==']'){
               if( stack.pop() != '[')
                   return 0;
           }
           if(c =='}'){
               if(stack.pop() != '{')
                   return 0;
           }
           }else{
               return 0;
           }
           //System.out.println(c);
       }
       if(stack.size()==0){
       return 1;
       }else{
           return 0;
       }

    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *