Valid Parentheses

Updated: Jan 20, 2021

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

  1. Open brackets must be closed by the same type of brackets.

  2. Open brackets must be closed in the correct order.

Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false Example 4: Input: s = "([)]" Output: false Example 5: Input: s = "{[]}" Output: true Constraints:

  • 1 <= s.length <= 104

  • s consists of parentheses only '()[]{}'.


class Solution {
    public boolean isValid(String s) {
     Map<Character,Character> map = new HashMap<Character,Character>();
        Stack<Character> stack = new Stack<>();
        for(char c:s.toCharArray())
                    if(stack.pop()!=map.get(c)) return false;
                return false;
        return stack.isEmpty();


13 views0 comments

Recent Posts

See All

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.