• + 0 comments

    Simple switch case within a for loop

    import java.util.*;
    class Solution{
    	
    	public static void main(String []argh)
    	{
    		Scanner sc = new Scanner(System.in);
    		
    		while (sc.hasNext()) {
    			String input=sc.next();
                //Complete the code
                System.out.println(isBalanced(input));
    		}
    		
    	}
        
        private static boolean isBalanced(String input) {
            if(input.length() == 0) {
                return true;
            }
            Stack<Character> stack = new Stack<>();
            for(Character c: input.toCharArray()) {
                switch(c) {
                    case '(' :
                    case '{' :
                    case '[' :
                        stack.add(c);
                        break;
                    case ')' :
                        if (stack.isEmpty() || !stack.pop().equals('('))
                            return false;
                        break;
                    case '}' :
                        if (stack.isEmpty() || !stack.pop().equals('{'))
                            return false;
                        break;
                    case ']' :
                        if (stack.isEmpty() || !stack.pop().equals('['))
                            return false;
                        break;
                    default:
                        return false;
                }
            }
            
            return stack.isEmpty();
        }
    }