#20. Valid Parentheses [LeetCode Grind 75 in Java]

[Problem Link] https://leetcode.com/problems/valid-parentheses/

class Solution {
    public boolean isValid(String s) {

        Stack<Character> st = new Stack<>();
        for(char c : s.toCharArray()){
            if(c == '(' || c == '{' || c == '[') st.push(c);
            else{
                if(st.isEmpty()) return false;
                char top = st.peek();
                if(c == ')' && top != '(') return false;
                if(c == '}' && top != '{') return false;
                if(c == ']' && top != '[') return false;

                st.pop();
            }

        }

        if(!st.isEmpty()) return false;
        return true;
    }
}