jeudi 19 mars 2020

plesase explain me this valid parentheis program using stack java [closed]

class Solution {

if valid parenthesis "()=true" ,"()[]{}=true","(]=false","([)]=false","{[]}=true

public boolean isValid(String s) {
 Stack<Character> p=new Stack<>();
 for(char c:s.toCharArray()){   
       if(c== '('){
           p.push(')');
       } 
        else if(c== '{'){
           p.push('}');
       } 
        else if(c== '['){
           p.push(']');
       } 
        else if(p.isEmpty()||p.pop()!=c){
            return false;
        }
    }  
    return p.isEmpty();
}

}

Aucun commentaire:

Enregistrer un commentaire