Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

nestparen challenge

Posted on 2016-09-21
4
Medium Priority
?
136 Views
Last Modified: 2016-09-22
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 41810247
You have to check if each opening bracket '(' in the string has a corresponding closing bracket ')'
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810913
i understood now. let me think
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811215
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?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41811978
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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question