@@ -0,0 +1,2978 @@
+package hacker_rank;%0A%0Aimport java.io.*;%0Aimport java.util.*;%0A%0Aclass BalancedParenthesis %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        %0A        // Define scanner%0A        Scanner in = new Scanner(System.in);            %0A        %0A        // No of test cases    %0A        int T = in.nextInt();%0A        %0A        // for each test case%0A        for(int t = 0; t%3CT; t++)%7B%0A            %0A            // input string%0A            String s = in.next();%0A            %0A            // define stack%0A            LinkedList%3CCharacter%3E stk = new LinkedList%3CCharacter%3E();    %0A            %0A            // assume balanced%0A            boolean balanced = true;%0A            %0A            // for each character%0A            for(int i=0;i%3Cs.length();i++)%7B%0A                %0A                char c = s.charAt(i);%0A                %0A                %0A                switch(c)%7B%0A                                // Add on stack if opening bracket%0A                    case '(':%0A                    case '%5B':%0A                    case '%7B': %0A                                stk.addFirst(c);%0A                                break;%0A                    %0A                                // If closing bracket, check if matching opening bracket is on top of stack.%0A                                // If not, expression is not balanced.%0A                                // If yes, remove matching opening bracket from stack%0A                    case ')':   if(stk.peekFirst()==null %7C%7C stk.peekFirst()!='(')%0A                                    balanced = false;%0A                                else%0A                                    stk.removeFirst();%0A                                 break;%0A                    %0A                    %0A                    case '%7D':   if(stk.peekFirst()==null %7C%7C stk.peekFirst()!='%7B')%0A                                    balanced = false;%0A                                else%0A                                    stk.removeFirst();%0A                                break;%0A                    %0A                    case '%5D':%0A                                if(stk.peekFirst()==null %7C%7Cstk.peekFirst()!='%5B')%0A                                    balanced = false;%0A                                else%0A                                    stk.removeFirst();%0A                                break;%0A                                                %0A                    %0A                    %0A                    %0A                %7D%0A                %0A                // If balanced flag was reset, stop checking other characters of this string%0A                if(!balanced)%7B%0A                    break;%0A                %7D%0A                    %0A                %0A                %0A            %7D%0A            %0A            // If stack is empty and balanced flag is still set, expression is balanced %0A            if(stk.size() == 0 && balanced)%0A                System.out.println(%22YES%22);%0A            else   // otherwise not balanced%0A                System.out.println(%22NO%22);%0A            %0A            %0A        %7D%0A    %7D%0A%7D

Solve Problem: Balanced Parenthesis