My solution in Java


  • 0
    X

    class Solution {
    public boolean isValid(String s) {
    Stack stack = new Stack();
    for(int i =0;i<s.length();i++){
    if(s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{')
    stack.push(s.charAt(i));
    else{
    if(!stack.empty()){
    char c = (char) stack.pop();
    if(c=='(')
    c =(char) (c +1);
    else
    c =(char) (c +2);
    if(c!=s.charAt(i))
    return false;
    }else{
    return false;
    }

    		}
    	}
        if(stack.empty())
            return true;
    	else
            return false;
    
    
    }
    

    }


Log in to reply
 

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