int main(void) { char s[11]; scanf("%s", s); bool r = nestParen (s, 0, 0); printf("%d\n", (int)r); return EXIT_SUCCESS; }
bool nestParen (char x[], int i, int z) { if (x[i] == 0 || z < 0) { return z == 0; } if (x[i] == '(') { z++; } else if (x[i] == ')') { z--; } return nestParen(x, i+1, z); }
// 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. bool nestParen( string str ) { if( str.empty() ) return true; if( str.length() >= 2 ) return ( str.at( 0 ) == '(' && str.at( str.length() - 1 ) == ')' ) && nestParen( str.substr( 1, str.length() - 2 ) ); return false; }