Given a string containing nested parentheses, write a function to check if the input string is valid and balanced.