## Problem:

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

## Solution:

public boolean nestParen(String str) {
if (str.equals("")) return true;
if (str.charAt(0) == '(' && str.charAt(str.length()-1) == ')')
return nestParen(str.substring(1,str.length()-1));
else
return false;
}

1. public boolean nestParen(String str) {
return str.length() == 0 ||
('(' == str.charAt(0) && ')' == str.charAt(str.length() - 1))
&& nestParen(str.substring(1, str.length() - 1));
}

2. if(str.equals("")) return true;
if(str.charAt(0)!='(' || str.charAt(str.length()-1)!=')' ) return false;
else return nestParen(str.substring(1,str.length()-1));

3. I also added a check so that it would return false immediately if number of symbols is odd:

if (str.length() % 2 != 0) return false;

4. if(str.startsWith("(")&&str.endsWith(")")){
return nestParen(str.substring(1,str.length()-1));
}
return str.equals("");
}

5. public boolean nestParen(String str) {
return str.length() == 0
|| str.charAt(0) == '('
&& str.charAt(str.length() - 1) == ')'
&& nestParen(str.substring(1, str.length() - 1));
}

6. public boolean nestParen(String str) {
if(str.startsWith("(")&&str.endsWith(")")){
return nestParen(str.substring(1,str.length() - 1));
}
return str.isEmpty();
}