HTML & JS
public static boolean Parenthesis(String s){
    if(s.length()%2 != 0) return false;

    Stack<Character> stack = new Stack();
    Map<Character,Character> mp = new HashMap<Character, Character>() {
        {
            put('(',')');
            put('[',']');
            put('{','}');
        }
    };

    for(char ch : s.toCharArray()){
        if(mp.containsKey(ch)){
            stack.push(ch);
        }else if(mp.containsValue(ch)){
            if(stack.isEmpty() || mp.get(stack.pop()) != ch){
                return false;
            }
        }
    }
    return stack.isEmpty();
}

Generated URL:

Compile Errors :

prog.java:1: error: class, interface, or enum expected
public static boolean Parenthesis(String s){
              ^
prog.java:4: error: class, interface, or enum expected
    Stack stack = new Stack();
    ^
prog.java:5: error: class, interface, or enum expected
    Map mp = new HashMap() {
    ^
prog.java:8: error: class, interface, or enum expected
            put('[',']');
            ^
prog.java:9: error: class, interface, or enum expected
            put('{','}');
            ^
prog.java:10: error: class, interface, or enum expected
        }
        ^
prog.java:13: error: class, interface, or enum expected
    for(char ch : s.toCharArray()){
    ^
prog.java:16: error: class, interface, or enum expected
        }else if(mp.containsValue(ch)){
        ^
prog.java:19: error: class, interface, or enum expected
            }
            ^
prog.java:23: error: class, interface, or enum expected
}
^
10 errors

Output:

No Output
Report Bug

Free Online Course By GeeksforGeeks