Valid Parentheses – Leetcode Solution

In this post, we are going to solve the 20. Valid Parentheses problem of Leetcode. This problem 20. Valid Parentheses is a Leetcode easy level problem. Let’s see code, 20. Valid Parentheses – Leetcode Solution.

We provide the solution to this problem in 3 programming languages i.e. Java, C++ & Python.

This Leetcode problem, 20. Valid Parentheses is often asked in coding interviews.

Problem

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.
  3. Every close bracket has a corresponding open bracket of the same type.

Example 1 :


Input: s = "()"
Output: true

Example 2 :


Input: s = "()[]{}"
Output: true

Example 3 :


Input: s = "(]"
Output: false

Constraints

  • 1 <= s.length <= 104
  • s consists of parentheses only '()[]{}'.

Now, let’s see the code of 20. Valid Parentheses – Leetcode Solution.

Valid Parentheses – Leetcode Solution

20. Valid Parentheses – Solution in Java

class Solution {
    
    public boolean handleClosing(Stack<Character> s, char openBracket){
        if(s.size() == 0){
            return false;
        }else if(s.peek() != openBracket){
            return false;
        }else {
            s.pop();
        }
        return true;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(char ch : s.toCharArray()){
            if(ch == '(' || ch == '{' || ch == '['){
                stack.push(ch);
            }else if(ch == ')'){
                boolean val = handleClosing(stack,'(');
                if(val == false) return false;
            }else if(ch == '}'){
                boolean val = handleClosing(stack,'{');
                if(val == false) return false;
            }else if(ch == ']'){
                boolean val = handleClosing(stack,'[');
                if(val == false) return false;
            }
        }
        
        if(stack.size() > 0)return false;
        return true;
    }
}

20. Valid Parentheses – Solution in C++

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        
        for(char c: s){
            if(c == '(' || c == '{' || c == '['){
                st.push(c);
            }
            else{
                if(st.empty()){
                    return false;
                }
                if(c==')' and st.top()=='('){
                    st.pop();
                }
                else if(c=='}' and st.top()=='{'){
                    st.pop();
                }
                else if(c==']' and st.top()=='['){
                    st.pop();
                }
                else{
                    return false;
                }
            }
        }
        if(st.empty()){
            return true;
        }
        return false;
    }
};

20. Valid Parentheses – Solution in Python

class Solution:
    def isValid(self, s: str) -> bool:
        valid_brack = [('{', '}'), ('(', ')'), ('[', ']')]
        stack = []
        for c in s:
            if len(stack)>0 and (stack[-1], c) in valid_brack:
                stack.pop()
            else:
                stack.append(c)
        return len(stack)==0

Note: This problem 20. Valid Parentheses is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *