Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

nestparen challenge

Hi,

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
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

i understood now. let me think
Avatar of gudii9

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;
}
}

Open in new window



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;
}

Open in new window