My JAVA Solution using Stack (beats 97.8%)


  • 0
    P

    '''

    public class Solution {
    public boolean isValid(String s) {
    if(s==null)
    return true;

        if(s.length()==1 || s.length()%2==1)
            return false;
    
        Stack<Character> st = new Stack<Character>();
        int i=0;
        char ch;
        while(i !=s.length())
        {
            ch =s.charAt(i++);
    
            if(ch == '(' || ch == '['||ch == '{')
                st.push(ch);
    
            else if(st.isEmpty())
                return false;
            
            else if((ch == ')' && st.peek()=='(') || (ch == '}' && st.peek()=='{') || (ch == ']' && st.peek()=='['))
                st.pop();
    
            else
                return false;
        }
        if(!st.isEmpty())
            return false;
        
        return true;
    }
    

    }

    '''


Log in to reply
 

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