Solved

nestparen challenge

Posted on 2016-09-21
4
57 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
  • 2
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now