gudii9
asked on
nestparen challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p183174
i was not clear on description. please advies
I am working on below challenge
http://codingbat.com/prob/p183174
Recursion-1 > nestParen
prev | next | chance
Given a string, return true if it is a nesting of zero or more pairs of parenthesis, like "(())" or "((()))". Suggestion: check the first and last chars, and then recur on what's inside them.
nestParen("(())") → true
nestParen("((()))") → true
nestParen("(((x))") → false
i was not clear on description. please advies
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public boolean nestParen(String str) {
if(str.equals("")){
return true;
}
else if( str.charAt(0)=='(' && str.charAt(str.length()-1) ==')' ){
return nestParen(str.substring(1,str.length()-1));
}
else{
return false;
}
}
above passes ll tests. any improvements or alternate approaches?
Not really.
public boolean nestParen(String str) {
if(str.length()==0){
return true;
}
if( str.charAt(0)=='(' && str.charAt(str.length()-1) ==')' ){
return nestParen(str.substring(1,str.length()-1));
}
return false;
}
ASKER