• + 1 comment

    A solution without using stack -

    import java.util.*; class Solution{

    public static void main(String []argh)
    {
        Scanner sc = new Scanner(System.in);
    
        while (sc.hasNext()) {
            String input=sc.next();
            int count = 0;
            //Complete the code
            if (input.length() % 2 == 0){
               for (int i = 0 ; i < input.length(); i++){
                    if (input.charAt(i) == '{' ||
                        input.charAt(i) == '[' ||
                        input.charAt(i) == '('){
                        count+=1;
                    }
                    else{
                        count-=1;
                    }
                    if(count < 0){
                        System.out.println("false");
                        break;
                    }
                } 
                if (count == 0){
                    System.out.println("true");
                }
            }
            else {
                System.out.println("false");
            }
    
        }
    
    }
    

    }