Given a string s 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.


&nbsp;
Example 1:

Input: s = "()"
Output: true


Example 2:

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


Example 3:

Input: s = "(]"
Output: false


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 104
	s consists of parentheses only '()[]{}'.


class Solution:
    def isValid(self, string: str) -> bool:
         while True:
            if '()' in string:
                string = string.replace('()', '')
            elif '{}' in string:
                string = string.replace('{}', '')
            elif '[]' in string:
                string = string.replace('[]', '')

            else:
                return not string
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>(); // create an empty stack
        for (char c : s.toCharArray()) { // loop through each character in the string
            if (c == '(') // if the character is an opening parenthesis
                stack.push(')'); // push the corresponding closing parenthesis onto the stack
            else if (c == '{') // if the character is an opening brace
                stack.push('}'); // push the corresponding closing brace onto the stack
            else if (c == '[') // if the character is an opening bracket
                stack.push(']'); // push the corresponding closing bracket onto the stack
            else if (stack.isEmpty() || stack.pop() != c) // if the character is a closing bracket
                // if the stack is empty (i.e., there is no matching opening bracket) or the top of the stack
                // does not match the closing bracket, the string is not valid, so return false
                return false;
        }
        // if the stack is empty, all opening brackets have been matched with their corresponding closing brackets,
        // so the string is valid, otherwise, there are unmatched opening brackets, so return false
        return stack.isEmpty();
    }
}
class Solution {
public:
// Ref:  balanced parenthesis video from LUV bhaiya YT channel 
unordered_map<char,int>symbols={{'[',-1},{'{',-2},{'(',-3},{']',1},{'}',2},{')',3}};
    stack<char>st;
    bool isValid(string s) {
        for(auto &i:s){
            if(symbols[i]<0){
                st.push(i);
            }
            else{
                if(st.empty()) return false;
                char top=st.top();
                st.pop();
                if(symbols[i]+symbols[top]!=0) return false;
            }
        }
    if(st.empty()) return true;
    return false;
        
    }
};
var isValid = function(s) {   
    const stack = [];
    
    for (let i = 0 ; i < s.length ; i++) {
        let c = s.charAt(i);
        switch(c) {
            case '(': stack.push(')');
                break;
            case '[': stack.push(']');
                break;
            case '{': stack.push('}');
                break;
            default:
                if (c !== stack.pop()) {
                    return false;
                }
        }
    }
    
    return stack.length === 0;
};
Valid Parentheses