hey so i am trying to write a code that that tells me if a string is valid or not . a valid string is a string that contains an equal number of "(" and ")" and each "(" must be closed by a ")" for example
'((()()))' this is a valid string . this isn't ')( '
this is what i wrote so far :
def is_valid_paren(s, cnt=0):
if s == "":
return True
if "(" in s:
cnt += 1
if ")" in s:
cnt -= 1
return is_valid_paren(s[1:])
it doesn't give the correct output for
"(.(a)"
yet for
"p(()r((0)))"
it does why does it sometimes work ? oh one more thing this is to be solved only by recursion ( without the use of loops anywhere )