[LeetCode #20] Valid Parentheses

Valid Parentheses
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.

The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.

public class Solution {
    public boolean isValid(String s) {
        
		java.util.Map<Character, Character> mp = new java.util.HashMap<Character, Character>();
		mp.put('(', ')');
		mp.put('[', ']');
		mp.put('{', '}');

		java.util.Stack<Character> stack = new java.util.Stack<Character>();

		for (int i = 0; i < s.length(); i++) {
			char currentCharacter = s.charAt(i);
			if (mp.keySet().contains(currentCharacter)) {
				stack.push(currentCharacter);
			} else if (mp.values().contains(currentCharacter)) {
				if (!stack.empty() && mp.get(stack.peek()) == currentCharacter) {
					stack.pop();
				} else {
					return false;
				}
			}
		}

		return stack.isEmpty();
	
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s