Valid Parentheses Leetcode Solution

In this post, we are going to solve the Valid Parentheses Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

Valid Parentheses Leetcode Solution
Valid Parentheses Leetcode Solution

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, lets see the leetcode solution of Valid Parentheses Leetcode Solution.

Valid Parentheses Leetcode 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

Valid Parentheses Leetcode Solution in CPP

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;
    }
};

Valid Parentheses Leetcode 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;
    }
}

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

NOTE: Merge Two Sorted Lists

Sharing Is Caring