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
Solved

nestparen challenge

Posted on 2016-09-21
4
96 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …

860 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