My java solution with Stacks.


  • 0

    Here it is:
    '''
    public class Solution {
    public boolean isValid(String s) {
    Stack<Character> St = new Stack<Character>();
    for(char c:s.toCharArray()){
    if(c=='(' || c=='{' || c=='[')
    St.push(c);
    else if(!St.empty()){
    if(c==')' && St.peek()=='(' )
    St.pop();
    else if(c=='}' && St.peek()=='{' )
    St.pop();
    else if(c==']' && St.peek()=='[' )
    St.pop();
    else
    return false;
    }
    else return false;
    }
    return St.empty();
    }

    }
    

    '''


Log in to reply
 

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