My easy Java solution


  • 0
    I
    public class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            for(int i = 0; i < s.length(); i++) {
                switch(s.charAt(i)) {
                    case '(': stack.push('(');break;
                    case '{': stack.push('{');break;
                    case '[': stack.push('[');break;
                    case ')': if(!stack.empty() && stack.pop().equals('(')) break; else return false;
                    case '}': if(!stack.empty() && stack.pop().equals('{')) break; else return false;
                    case ']': if(!stack.empty() && stack.pop().equals('[')) break; else return false;
                }
            }
            return stack.empty();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.