Java Solution -- Using a single List.


  • 1
    W
    public class Solution {
        public boolean isValid(String s) {
            List<String> st = new ArrayList<String>();
            
            for(int i = 0; i < s.length(); i++){
                if(s.charAt(i) == '(' || 
                    s.charAt(i) == '[' ||
                    s.charAt(i) == '{'){
                    st.add(String.valueOf(s.charAt(i)));   
                }else{
                    if(s.charAt(i) == '}'){
                        if(st.size() == 0)
                            return false;
                        if(!st.get(st.size()-1).equals("{")){
                            return false;
                        }else{
                            st.remove(st.size()-1);
                        }
                    }else if(s.charAt(i) == ']'){
                        if(st.size() == 0)
                            return false;
                        if(!st.get(st.size()-1).equals("[")){
                            return false;
                        }else{
                            st.remove(st.size()-1);
                        }
                    }else if(s.charAt(i) == ')'){
                        if(st.size() == 0)
                            return false;
                        if(!st.get(st.size()-1).equals("(")){
                            return false;
                        }else{
                            st.remove(st.size()-1);
                        }
                    }
                }
            }
            
            if(st.size() != 0)
                return false;
            return true;
        }
    }

  • 0
    A

    The method s.charAt() return a value of charater,isn't it? Incompatible operand types char and String??


  • 0
    W

    Yes. s.charAt() returns a char value. That is why the comparison syntax is as follows:-
    s.charAt(i) == ')'
    *Notice the single quote? That indicates that the toBeCompared value is in the form of a char.


  • 0
    A

    So that explains it.Thanks for sharing.


  • 0
    W

    No problem. :)


  • 1
    S
    public class Solution {
        public boolean match(char c1, char c2) {
            boolean b1 = (c1 == '(' && c2 == ')') || (c2 == '(' && c1 == ')');
            boolean b2 = (c1 == '[' && c2 == ']') || (c2 == '[' && c1 == ']');
            boolean b3 = (c1 == '{' && c2 == '}') || (c2 == '{' && c1 == '}');
            return b1 || b2 || b3;
        }
        
        public boolean isLeft(char c) {
            return (c == '{' || c == '[' || c == '(');
        }
        
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<Character>();
            for (int i = 0; i < s.length(); i++) {
                if (isLeft(s.charAt(i))) {
                    stack.push(s.charAt(i));
                } else {
                    if (stack.empty() || !match(stack.pop(), s.charAt(i))) 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.