Solved

nestparen challenge

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …

730 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