Skip to content

Latest commit

 

History

History
52 lines (45 loc) · 1.11 KB

20.Valid Parentheses.md

File metadata and controls

52 lines (45 loc) · 1.11 KB

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

An input string is valid if:

Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true
Example 2:

Input: "()[]{}"
Output: true
Example 3:

Input: "(]"
Output: false
Example 4:

Input: "([)]"
Output: false
Example 5:

Input: "{[]}"
Output: true
class Solution {
    public boolean isValid(String s) {
        Map<Character,Character> map=new HashMap<>();
        map.put(')','(');
        map.put(']','[');
        map.put('}','{');
        Stack<Character> stack=new Stack<>();
        for(char ch:s.toCharArray()){
            if(map.containsKey(ch)){
                if(!stack.isEmpty() && stack.peek()==map.get(ch)){
                    stack.pop();
                }
                else
                    return false;
            }
            else
                stack.push(ch);
        }
        return stack.size()==0?true:false;
    }
}