Stackless solution.


  • 0
    V

    Stackless solution:

    boolean solve2(String in) {
    while(true){
    	String tmp=in.replace("[]","").replace("{}","").replace("()", "");
    	if( tmp.length()<in.length() ) in=tmp;
    	else break; // terminating condition: string did not get shorter
    }
    return in.isEmpty();
    

    }

    and recursive variation:

    boolean solve3(String in) {
    	return solve3recursively(in,in.length()+1);
    }
    
    boolean solve3recursively(String in, int len) {
    	if( in.length()>=len ) { return in.isEmpty(); } // terminating condition: string did not get shorter
    	return solve3recurrent(in.replace("[]","").
    			replace("{}","").replace("()", ""), in.length());
    }
    

    the end


Log in to reply
 

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