• + 0 comments

    public static void main(String []argh) { Scanner sc = new Scanner(System.in);

        while (sc.hasNext()) {
            String input=sc.next();
            String[] values = input.split("");
            Stack<String> stack = new Stack<>();
            Map<String, String> chars = new HashMap<>();
            chars.put("(", ")");
            chars.put("{", "}");
            chars.put("[", "]");
            boolean malformed = false;
    
            for (String value: values){
                if (chars.keySet().contains(value)){
                    stack.add(value);
                } else {
                    try{
                        String openingChar = stack.pop();
                        if (!chars.get(openingChar).equals(value)){
                            malformed = true;
                        }    
                    } catch (EmptyStackException e){
                        malformed = true;
                    }
    
                }
            }
            System.out.println(stack.isEmpty() && !malformed ? "true" : "false");
    
            //Complete the code
        }
    
    }