Clean JAVA Code


  • 0
    A
    public boolean isValid(String s) {
    	Map<Character, Character> map = new HashMap<Character, Character>();
    	map.put('(', ')');
    	map.put('{', '}');
    	map.put('[', ']');
    	Stack<Character> stack = new Stack<Character>();
    	for (Character ch : s.toCharArray())
    		if (map.containsKey(ch))
    			stack.push(map.get(ch));
    		else if (stack.isEmpty() || ch != stack.pop())
    			return false;
    	return stack.isEmpty();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.