Simple recursive solution ( java )


  • 0
    S
    public boolean isValid(String s) {
        
        if (removePairs(s).isEmpty()){
            return true;
        }else {
            return false;
        }
    }
    public static  String removePairs(String s){
    
        String s_copy = s;
    
        s = s.replace("()","");
        s = s.replace("[]","");
        s = s.replace("{}","");
    
    
        if (s.equals(s_copy) || s.isEmpty()){
            return s;
        }else {
            return removePairs(s);
        }
    }

Log in to reply
 

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